Find the reason why I can't save files on the server (But localhost saves ) images to the /public/uploads folder, it exists and its access rights are drwxrwxrwx. There are no errors, the api writes that everything is successful, it saves the file name to the database, but there are no files in the /public/uploads folder itself.
My configuration : next.js - prisma
My api (next-connect):
const upload = multer({
storage: multer.diskStorage({
destination: './public/uploads',
filename: (req, file, cb) => cb(null, file.originalname),
}),
});
const apiRoute = nextConnect<NextApiRequest & { file?: Express.Multer.File }, NextApiResponse>({
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.single('image'));
apiRoute.use(helmet());
apiRoute.post(async (req, res, next) => {
const token = req.cookies['sid']
const admin = await checkSession(token)
if (admin) {
return next()
}
next(new Error('Auth required'))
}, async (req, res) => {
try {
const img: string = req.file ? req.file.filename : ''
const loginSchema = z.object({
title: z.string().min(2).max(50),
shortDesc: z.string().min(2).max(150),
description: z.string(),
filterMainPeople: z.string().min(2).max(60),
detailFilterBrand: z.string().min(2).max(60),
detailFilterMode: z.string().min(2).max(60),
price: z.string().max(60),
})
const {
title, shortDesc, description, filterMainPeople, detailFilterBrand, detailFilterMode, price
} = loginSchema.parse(req.body)
const newOffer = await db.offer.create({
data: {
title,
shortDesc,
description,
filterMainPeople,
detailFilterBrand,
detailFilterMode,
img,
price,
active: true,
}
})
res.status(200).send(newOffer)
} catch (error) {
console.error(error)
if (error instanceof ZodError) {
return res.status(404).send({ message: "validation error" })
}
res.status(500).send({ message: "server error" })
}
});
export default apiRoute;
export const config = {
api: {
bodyParser: false, // Disallow body parsing, consume as stream
},
};
publicfolder of Next.js. Check out vercel.com/guides/how-to-upload-and-store-files-with-vercel.