I’m new to elysiajs and trying to upload files using multer and elysia, but its not working for me
i tried to add a beforehandle and afterhandle but it’s still not working ,
……………………………………………………………..
const storage = multer.diskStorage({
destination: "./uploads/",
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const upload = multer({
storage: storage,
limits: { fileSize: 1000000 }, // Limit file size to 1MB
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
},
}).single("myFile"); // 'myFile' is the name of the file input field in the HTML form
// Check file type
function checkFileType(file: any, cb: any) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
export const users = new Elysia({ prefix: "/files" })
// .post("/s", upload);
.guard(
{
beforeHandle() {
upload;
},
},
(users) =>
users
.post("/s", (req: any, res: any) => filesUpload(req, res))
.get("/ss", () => "f")
);
New contributor
omarghandour is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.