Skip to content

Parsing Queries

How to parse query parameters

In an Express.js application, query parameters can be parsed from the URL using the req.query object. Query parameters are usually included in the URL after the ”?” character and separated by ”&” (https://www.google.com/searchq=what+is+query+parameter+in+node+js&sca_esv=589601647).

Here’s a simple example:

const express = require('express');
const app = express();
const port = 3000;
app.get('/api/user', (req, res) => {
// Access query parameters from req.query
const userId = req.query.id;
const name = req.query.name;
// Process the parameters as needed
const user = {
id: userId,
name: name,
};
// Send a JSON response with the parsed parameters
res.json({ user });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});