One of the most useful and simple middleware components is the query
middleware. The query
middleware converts a query string from a URL into a JavaScript object and stores it as the query
property on the Request
object. As of Express 4.x, the functionality exists within built-in request parser without the need for additional middleware.
The following code snippet shows the basics of utilizing the query
middleware:
var express = require('express'),
var app = express();
app.get('/', function(req, res) {
var id= req.query.id;
var score = req.query.score;
console.log(JSON.stringify(req.query));
res.send("done");
});
The query string for the request would look like ?id=10&score=95
. Notice that JSON.stringify
can be called on req.query
because it is a JavaScript object.