0

I have the following Typescript code:

import express = require('express');

const app: express.Application = express();
const port: number = 3000;


app.listen(port, () => {
    console.log("Server started on port" + port);
    app.use(bodyParser.urlencoded({ extended: true }));
})

app.post('*', (req, res) => {
    console.log(req.body);
});

For some reason req.body is always undefined instead of getting the key-value pairs sent via postman. Adding app.use(bodyParser.urlencoded({ extended: true })); only changes the body from undefined to {}.

What could be the issue in this case?

1 Answers1

0

The line app.use(bodyParser.urlencoded({ extended: true })); must be called before app.listen. If you are using express 3.0 and below use the following code instead as answered here:

const app = express.createServer();
app.use(express.bodyParser());

If still doesn't work make sure in Postman that you are sending the data as application/x-www-form-urlencoded

postman form data send