* Expanded the solution for the 'Get Route Parameter Input from the Client' challenge * Expanded the guide for the 'Use body-parser to Parse POST Requests' challenge * Rewritten guide for the 'Serve JSON on a Specific Route' challenge and fixed source link * Expanded the guide for the 'Serve Static Assets' challenge * Expanded solution to the 'Get Query Parameter Input from the Client' challenge and fixed links to source file * Added solution to the 'Chain Middleware to Create a Time Server' challenge and fixed link to source file * Rewrite the 'Start a Working Express Server' challenge * Expanded the guide for 'Expand Your Project with External Packages from npm' * Added reference to semantic versioning in 'Add a Version to Your package.json' * fix/remove-links+fix-solutions * fix/remove-more-links
867 B
867 B
title
title |
---|
Get Query Parameter Input from the Client |
Get Query Parameter Input from the Client
Given the endpoint URL, /name?first=firstname&last=lastname
, we can extract the query parameters (first
and last
) and their corresponding values from the req.query
object and send a custom JSON response containing values derived from the query parameters to the client.
Solution
app.get("/name", function(req, res) {
var firstName = req.query.first;
var lastName = req.query.last;
// OR you can destructure and rename the keys
var { first: firstName, last: lastName } = req.query;
// Use template literals to form a formatted string
res.json({
name: `${firstName} ${lastName}`
});
});