9951 explained code solutions for 126 technologies


expressjsHow can I use Express.js to parse query strings?


Express.js is a web application framework for Node.js that allows you to easily parse query strings. To use Express.js to parse query strings, you need to first install the Express.js package with npm install express.

You can then create an Express.js application and use the app.use() method to parse the query string. The following example code will parse a query string and print the result to the console:

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

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

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

app.listen(3000);

If the query string contains a parameter named name with the value John, the output of the above code will be:

{ name: 'John' }

The code above consists of the following parts:

  1. const express = require('express');: This line imports the Express.js package.
  2. const app = express();: This line creates an Express.js application.
  3. app.use(express.urlencoded({ extended: true }));: This line enables the Express.js application to parse query strings.
  4. app.get('/', (req, res) => { ... });: This line defines a route handler for the root path that will print the query string to the console.
  5. app.listen(3000);: This line starts the Express.js application and listens for requests on port 3000.

For more information, see the Express.js documentation.

Edit this code on GitHub