Express.js: Request Parameters

·

3 min read

Table of contents

Request parameters are additional data attached in the request URL sent to the server. Server then provides customized response according to that parameter provided with request.

Route Params

It is extra info attached in request URL by using ':' symbol as prefix. Let's us assume product data being exported from product-data.js and see a code example. If it seems a lot. Please, don't get overwhelmed. I'll explain it in detail.

const express = require("express");
const app = express();
const products = require("./product-data.js");
// Home Page
app.get("/", (req, res) => {
    res.send("Home Page");
});
// Route format for request params
app.get("/api/products/:pid", (req, res) => {
    const {pid} = req.params;
    // Return product data of matching ID
    const productDetail = products.find(product => {
        product.id === Number(pid)
    });
    res.json(productDetail);
});
app.listen(5000, () => console.log("Listening on Port 5000"));
  • Initially, above express app simply handles GET request on '/' route and responds as 'Home Page'.

  • Then, our app also handles GET request on route which includes :pid at the end which is a custom parameter name given to indicate product id.

  • The req.params stores all the parameters specified in the route. In this case, it is pid which is retrieved by destructuring req.params.

  • That string pid value is converted to Number and compared to provide details of respective product.

  • For example: User requests for /api/products/3, then pid becomes 3 and detail of product with id of 3 is returned as response.

Query String

It is also a an extra data attached in the URL but the difference is that it's attached in the form of key: value pair. We can use this to send small amount of data to the server when requesting response. The '?' symbol denotes the starting on a query string in URL. I have discussed in detail about the meaning and format of query string in URL on my PHP blog.

Let's see a code example where user searches for particular product in our app:

const express = require("express");
const products = require("./product-data.js");
const app = express();
// No need to specify that we're going to use query string.
app.get("/api/products/search", (req, res) => {
    // Query given by user gets stored in 'req.query'
    const {name} = req.query;            
    if(name){
            const searchedProduct = products.filter(product => {
            if(product.name.includes(name)) return true;
        });
        res.json(searchedProduct);
    }
    else res.send(`Please provide product name for query`);
});

app.listen(5000, () => console.log("Listening to Port 5000!!!"));
  • Let's assume user sends following query string from frontend app: /api/products/search?name=Smartphone

  • Here, name is the key and Smartphone is the value. The name key is stored in req.query which we can use to filter the product from product data.

  • If the user doesn't provide any query, then we respond to the user by informing to provide the query.

Hope this was helpful to you! That's all about this blog.