Sometimes, we want to access POST form fields with Node.js and Express.
In this article, we’ll look at how to access POST form fields with Node.js and Express.
How to access POST form fields with Node.js and Express?
To access POST form fields with Node.js and Express, we can use the body-parser
package.
To install it, we run
npm install --save body-parser
Then we write
const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
to require the body-parser
package.
And then we call app.use
with bodyParser.json()
to let us parse JSON payload from POST requests in our routes.
And we call bodyParser.urlencoded
to let us parse URL encoded POST request payloads in our routes.
Conclusion
To access POST form fields with Node.js and Express, we can use the body-parser
package.