My app.js:
const multer = require("multer");
const path = require("path");
const storage = multer.diskStorage({
destination: './upload/images',
filename: (req, file, cb) => {
return cb(null, ${file.fieldname}_${Date.now()}${path.extname(file.originalname)}
)
}
})
const upload = multer({
storage: storage,
limits:{
fileSize: 10485760
}
})
app.use('/profile', express.static('upload/images'));
app.post("/upload", upload.single('profile'), (req, res) => {
res.render({
success: 1,
profile_url: http://localhost:3000/profile/${req.file.filename}
})
var imgPath = req.file.path;
})
function errHandler(err, req, res, next) {
if (err instanceof multer.MulterError) {
res.json({
success: 0,
message: err.message
})
}
}
app.use(errHandler);
My Ejs file:
<%= imgPath%>
Hope you all guys are doing good. I am new to node js, express js, ejs, I am stuck in this problem for 2 days now and I am not able to figure out where I am wrong. :(
Top comments (1)
The variable that you're passing into your ejs file in the render method is profile_url. You need to pass the variable you wish to the render method.