Get URL Parameters and Query Strings in Node JS
Summary:Many basic Node JS tutorials explain how to get URL parameters in Node JS but leave the query parameters part. This tutorial explains how to extract or get route URL query strings and parameters.
It is a very common problem to extract the query strings and parameters from the route URL in Node JS.
Before we can try to understand we must know What are Query Strings and Route Parameters?
Route Parameters in Node JS:
It is not possible to create a route for each and every page in Node JS.
Suppose we are building an e-commerce application then we don’t need to define an individual route for each product.
Instead, we define a route with a parameter and extract that parameter in the controller and process further based on that.
For Example:
app.get('/product/:productId', (req, res)=>{
//Extract productId parameter and process further
const productId = ?????
const data = db.getProductWithProductId(productId);
res.send(data);
})
Query Parameters in Node JS:
Same as route parameters we have query parameters which are characteristics to a route.
Say we have three colours of a product then instead of nested route parameters we can simply add a query parameter color=red
to the route.
/product/some-product-id?color=red
Here I have tried to explain the same with and without using the Express JS framework.
Express JS - Get URL Parameters and Query Strings
It is very simple to extract URL parameters and query strings with Express JS with just one line of code.
Extract URL Parameters
req.params.productId // Here productId is our route parameter
Here is the complete example for your understanding.
app.get('/product/:productId', (req, res)=>{
//Extract productId parameter and process further
const productId = req.params.productId; //<<<<<<<<<<--------------
const data = db.getProductWithProductId(productId);
res.send(data);
})
Extract URL Query Strings
req.query.color
Here is the complete example for better understanding.
app.get('/product/:productId', (req, res)=>{
//Extract productId parameter and process further
const productId = req.params.productId;
const color = req.query.color; //<<<<<<<<<<--------------
const data = db.getProductWithProductId(productId, color);
res.send(data);
})
Extract Route Parameters and Query Strings Without Express JS
Extract URL Parameters
This process is the same as above.
req.params.productId // Here productId is our route parameter
Extract Query Params
The process is a bit different. We need to install two additional packages for this purpose.
npm install url querystring --save
Now we can move to actual code.
const url = require('url');
const querystring = require('querystring');
let routeUrl = 'https://holycoders.com/products/some-product-id?color=red';
let parsedRouteUrl = url.parse(routeUrl);
let requiredQueryString = querystring.parse(parsedRouteUrl.query);
If you have any problem regarding this tutorial you can let us know in the comments.
Join Our Youtube Channel
Subscribe