I am sending form data values using Postman as given here. Postman Request. I am making use of NodeJS server in the backend to get the data from this POST request.
The code for my app.js file is given below.
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(bodyParser.json({
limit: '50mb',
}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}));
const { multerUploadOneFile } = require(path.resolve(__dirname, 'helpers', 'multerConfig'));
app.post('/upload', function (req, res) {
// **Here I need to get the other parameters in the request body, i.e ```userId```, ```fileType```
console.log(req.body);
multerUploadOneFile(req, res, function (error) {
if (error) {
return res.status(401).send({
status: 'failure',
message: error.message
});
}
return res.status(200).send({
status: 'success',
message: 'successfully uploaded file.',
});
})
});
I am logging the request body in /upload api endpoint but I am not able to get anything. Please let me know how can I get the req.body in callback of /upload api endpoint.
body-parser doesn't support multi-part bodies.
multer (which you're correctly using) does, however it needs to execute and parse the request before you'll be able to access anything.
multerUploadOneFile needs to execute before you have acceess to .body or .file.
app.post('/upload', function (req, res) {
multerUploadOneFile(req, res, function (error) {
console.log(req.body, req.file);