hoangvvo/next-connect

Not able to use multer as middleware

ABHI190701 opened this issue · 0 comments

import { createRouter, expressWrapper, createEdgeRouter } from "next-connect";
import { NextApiRequest, NextApiResponse } from "next";
import { NextRequest, NextResponse } from "next/server";
import multer from "multer";
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/uploads");
  },
  filename: function (req, file, cb) {
    console.log("----FILE DETAILS----", file);
    const uniqueSuffix = Date.now();
    cb(null, uniqueSuffix + file.originalname);
  },
});

const upload = multer({ storage: storage }).single('image');

const router = createRouter<NextApiRequest, NextApiResponse>();

router.use(upload);
router.post(req) {
    return NextResponse.json({message: 'SUCCESS'})
  }

export default router.handler({
    onError: (err, req, res) => {
      console.error(err);
    },
  });