Parsing URL & Query Params and sending back appropriate data in NodeJS with Express & MongoDB

Tehseen Ahmed

Self Employeed MERN Stack Developer

Let's Talk About Url and Query Parameters

URL Params or Route Params!

  • 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.

​​​

Route path: /users/:userId/books/:bookId

Request URL: http://test.com/users/34/books/8989

req.params: { "userId": "34", "bookId": "8989" }

Query Params!

  • In simple terms, a query string is the part of a URL (Uniform Resource Locator) after the question mark (?).

  • It is meant to send small amounts of information to the server via the URL.

  • This information is usually used as parameters to query a database, or maybe to filter results. It's really up to you what they're used for.
Request URL: http://test.com/?username=ali&age=18

Parsing URL Params

By Tehseen Ahmed

Parsing URL Params

Parsing URL Params and sending back appropriate data

  • 493