How can I read data received in application / x-www-form-urlencoded format on a Node server?

I get the data at the web hosting URL as a POST request. Please note the content type of this request application/x-www-form-urlencoded.

This is a server to server request. And on my Node server, I just tried to read the received data using req.body.parameters, but the resulting values ​​are "undefined" ?

So how can I read data request data? Do I need to analyze the data? Do I need to install any npm module? Can you write a piece of code explaining this case?

+4
source share
1 answer

Express.js - Node.js, ExpressJS body-parser.

.

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

// With body-parser configured, now create our route. We can grab POST 
// parameters using req.body.variable_name

// POST http://localhost:8080/api/books
// parameters sent with 
app.post('/api/books', function(req, res) {
    var book_id = req.body.id;
    var bookName = req.body.token;
    //Send the response back
    res.send(book_id + ' ' + bookName);
});
+4

Source: https://habr.com/ru/post/1669227/


All Articles