9951 explained code solutions for 126 technologies


expressjsHow do I use Express.js to parse request parameters?


Express.js provides a built-in middleware function called express.urlencoded which can be used to parse request parameters.

The following example code will parse the request parameters that are sent in a POST request body and log them to the console:

const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));

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

The output of the above code will be an object containing the request parameters, for example:

{ name: 'John', age: '30', email: '[email protected]' }

The express.urlencoded middleware function takes an optional object as a parameter. The extended property of this object can be set to true to use the qs module for parsing the request parameters.

Helpful links

Edit this code on GitHub