Let's take a look at an example of us getting query parameters in a route: const express = require( 'express') Ĭonst bodyParser = require( 'body-parser') Ĭonst querystring = require( 'querystring') Ĭonst Article = require( './models').Article Īpp.use(bodyParser.urlencoded(, just like the query object.Īs you can see, we again just take our parameter directly from an object contained within the request object. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object. It is in the form of an object in which you can directly access the query parameters you care about. Your query parameters can be retrieved from the query object on the request object sent to your route. While query parameters are typically used in GET requests, it's still possible to see them in POST and DELETE requests, among others. We'd like to extract both the page and limit parameters so we know which articles to return to the page that the user requested. Now, taking the same example from above: This is a pretty common use-case in Express, and any HTTP server, so hopefully the examples and explanation I show here are clear. Now, let's move on to the first main purpose of this article - how to extract these from our Express request object. The query parameters are the actual key-value pairs like page and limit with values of 2 and 3, respectively.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |