Reputation: 3272
I've got a simple Express server that uses the body-parser
module to access POST-parameters. The app looks like this:
'use strict';
const express = require('express');
const app = express();
const apiRouter = require('./api/routes.js');
// Set our port for the server application
const port = process.env.PORT || 8080;
// Register the routes for the /api prefix
app.use('/api', apiRouter);
// Start server
app.listen(port);
console.log('The server is running on port ' + port);
'use strict';
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
// Configure app to use bodyParser(). This will let us get the data from a POST
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
// START ROUTES
router.post('/devices', (req, res) => {
console.log(req.body); // Returns {}
res.json(req.body);
});
module.exports = router;
The problem is that the req.body
object is empty (Always returns an empty object {}
). Since I already loaded the body-parser
middleware I have no idea what else I can try. I hope you have any suggestions.
Upvotes: 0
Views: 1417
Reputation: 3272
I used the app Postman for testing. It appeared that it sent the POST data as form-data
instead of x-www-form-urlencoded
. After changing this setting the data showed up.
Upvotes: 2