Route Params in expressjs

Introduction

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in t…


This content originally appeared on DEV Community and was authored by Naftali Murgor

Introduction

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys. Expressjs official Docs

Let's say we defined a route(see the previous article) in our app in the example code:

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

// a route that takes params:
app.get('/users/:userId/books/:bookId', (req, res) => {
  // we can extract parameters from the route from req.params object
  const userId = req.params.userId
  const bookId = req.params.bookId
  // use userId and bookId values to do something useful
})

Maps to something like this:

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

Important Note:

It’s important to sanitize and validate any input coming from the client requests. Requests are user-constructed data and can contain anything. There are libraries that can be used to perform sanitization for every possible kinds of data.

Summary

Route params are useful if we need to pass data to our app within a Request URL. From our app, we may extract these values and look up the item or more data from a Redis store, etc and return meaningful data inside the HTTP Response

Always remember to sanitize and validate any data that comes in from a Request. Requests are user-constructed and may contain anything.

Next we shall dive into:
Next, we shall dive into:

  1. Post Requests in detail
  2. Route handlers
  3. Middleware - How middlewares make Express robust.

Thanks for stopping by! Happy new year and may the energy be with you!


This content originally appeared on DEV Community and was authored by Naftali Murgor


Print Share Comment Cite Upload Translate Updates
APA

Naftali Murgor | Sciencx (2021-12-28T15:20:51+00:00) Route Params in expressjs. Retrieved from https://www.scien.cx/2021/12/28/route-params-in-expressjs/

MLA
" » Route Params in expressjs." Naftali Murgor | Sciencx - Tuesday December 28, 2021, https://www.scien.cx/2021/12/28/route-params-in-expressjs/
HARVARD
Naftali Murgor | Sciencx Tuesday December 28, 2021 » Route Params in expressjs., viewed ,<https://www.scien.cx/2021/12/28/route-params-in-expressjs/>
VANCOUVER
Naftali Murgor | Sciencx - » Route Params in expressjs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/28/route-params-in-expressjs/
CHICAGO
" » Route Params in expressjs." Naftali Murgor | Sciencx - Accessed . https://www.scien.cx/2021/12/28/route-params-in-expressjs/
IEEE
" » Route Params in expressjs." Naftali Murgor | Sciencx [Online]. Available: https://www.scien.cx/2021/12/28/route-params-in-expressjs/. [Accessed: ]
rf:citation
» Route Params in expressjs | Naftali Murgor | Sciencx | https://www.scien.cx/2021/12/28/route-params-in-expressjs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.