Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active November 20, 2021 00:10
Show Gist options
  • Save codeBelt/5522e88edf3d34d595bdf14cac6f163d to your computer and use it in GitHub Desktop.
Save codeBelt/5522e88edf3d34d595bdf14cac6f163d to your computer and use it in GitHub Desktop.
import nextConnect from 'next-connect';
import multer from 'multer';
const upload = multer({
storage: multer.diskStorage({
destination: './public/uploads',
filename: (req, file, cb) => cb(null, file.originalname),
}),
});
const apiRoute = nextConnect({
onError(error, req, res) {
res.status(501).json({ error: `Sorry something Happened! ${error.message}` });
},
onNoMatch(req, res) {
res.status(405).json({ error: `Method '${req.method}' Not Allowed` });
},
});
apiRoute.use(upload.array('theFiles'));
apiRoute.post((req, res) => {
res.status(200).json({ data: 'success' });
});
export default apiRoute;
export const config = {
api: {
bodyParser: false, // Disallow body parsing, consume as stream
},
};
@a4addel
Copy link

a4addel commented Jul 15, 2021

I was searching for the for like 4 months not, I quit Nextjs and just used React for uploading Middleware,

@izlemicom
Copy link

There is a problem about caching, I cant see the uploaded file immediately in production. I have to restart the server after every upload. How can I prevent it.

@izlemicom
Copy link

Deepin Ekran Görüntüsü_alan-seç_20211120030826
Deepin Ekran Görüntüsü_alan-seç_20211120030858

@izlemicom
Copy link

As seen in screenshots, only I see the image when I npm start again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment