It seems that the concept of "normal URLs" is subjective, with different styles existing but none being inherently more normal than the others.
There are various methods to pass parameters in a URL.
#1 - Embedding parameters in the URL path
https://someserver/things/0
https://contacts.google.com/person/c3429579852656514228
This approach adheres to RESTful design principles, where the URLs feature a noun followed by an ID identifying the specific entity.
#2 - Using query parameters for variable URL components
For the same example URLs mentioned earlier:
https://someserver/things?id=0
https://contacts.google.com/person?id=c3429579852656514228
Both design approaches have their own merits depending on the context, and it's even possible to combine them (e.g., incorporating optional query parameters into RESTful API designs). Neither method can be considered the standard; they represent different URL design strategies.
Express supports both approaches. When dealing with restful parameters within the URL path, you can use the :id
syntax and access the value using req.params.id
:
app.get('/things/:id', (req, res) => {
console.log(req.params.id);
res.send(req.params.id);
});
For query parameters, you don't specify them in the Express route path. The framework automatically parses any query parameters and makes them available through req.query
.
// Example: /things?id=789&sort=alpha
app.get('/things', (req, res) => {
console.log(req.query.id); // "789"
console.log(req.query.sort); // "alpha"
res.send(req.query.id);
});
Any query parameter present in the URL will be included in the req.query
object.
Usually, query parameters are treated as optional, so there's no need to include them in your route definition. If a specific query parameter is required, you must check its presence in the request and handle errors or trigger next()
to proceed to other request handlers if it's missing.
Additionally, there's another style that often involves client-side code to construct pages and includes URL arguments in hash tags. However, we'll save that topic for another discussion.