I recently attempted to send form data to my express js server. Below is the code snippet from my pug file:
form(action="/profile" method="post" enctype="multipart/form-data")
input(type="file" accept="image/*" name="profileimage")
input(type="text" name="username")
input(type="submit" value="Upload")
Afterwards, I tried to access the post data using req.body
in my server-side JavaScript file (profile.js):
router.post('/', function (req, res) {
console.log('Body- ' + JSON.stringify(req.body));
});
However, when I checked the console, this was the result:
body- {}
Interestingly, I noticed that if I remove enctype="multipart/form-data"
from the form declaration like
form(action="/profile" method="post")
, then I am able to successfully retrieve the posted data in the console.