Query vs. URL Parameters in Express.js
By: Yrjguygjytjg Yoyitbifbitjf Figgjgitgigfuykhfu
Tags: Backend, Nodejs, Web Development, Expressjs, JavaScript

A frequently asked question is whether you should use query parameters or URL parameters for an API — and where the differences between the two lie, completely independent of the Express.js framework.

Here are all the answers and code samples you need to understand the differences and applications and build your next Express.js API with them.

Basic Syntax

Let’s first have a look at how the two are structured.

Query Parameter: domain.com/path?key=value

URL Parameter: domain.com/path/value

In both cases, the path is optional, of course, we can also accept parameters for the root-URL ( domain.com/ ).

Real examples:

Query Parameter: domain.com/products?search=tomato

URL Parameter: domain.com/products/tomato

In both cases, tomato is the value we send to the webserver.

The differences — and when to use what

Simply put, we can do the same thing with both — there are technically no differences, so we could use the same API with query parameters or with URL parameters (also called route parameters). As you will see in a moment, you can pass multiple data with both.

So in the end, it is up to you. However, as for almost everything else, there are standards. Following them makes sense — first of all, you know the standards themselves and can better understand the code of others. Secondly, following these standards, you can write your own code that is understood by others.

So here are the standards:

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb1.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb2.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb3.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m0.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m1.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m2.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m3.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m4.html

http://landrover.gi/land/gp/rex-r-v-m0.html

http://landrover.gi/land/gp/rex-r-v-m1.html

http://landrover.gi/land/gp/rex-r-v-m2.html

http://landrover.gi/land/gp/rex-r-v-m3.html

http://landrover.gi/land/gp/rex-r-v-m4.html

https://sv.tie.org/sv/gp/rex-r-v-m0.html

https://sv.tie.org/sv/gp/rex-r-v-m1.html

https://sv.tie.org/sv/gp/rex-r-v-m2.html

https://sv.tie.org/sv/gp/rex-r-v-m3.html

https://sv.tie.org/sv/gp/rex-r-v-m4.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es02.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es03.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es04.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es05.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es06.html

http://landrover.gi/land/gp/E-M-v-V-es02.html

http://landrover.gi/land/gp/E-M-v-V-es03.html

http://landrover.gi/land/gp/E-M-v-V-es04.html

http://landrover.gi/land/gp/E-M-v-V-es05.html

http://landrover.gi/land/gp/E-M-v-V-es06.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de000.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de001.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de002.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de003.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de004.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de005.html

https://sv.tie.org/sv/atp/Thiem-v-Tsitsipas-liv-Mara-de006.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de000.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de001.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de002.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de003.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de004.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de005.html

http://landrover.gi/land/atp/Thiem-v-Tsitsipas-liv-Mara-de006.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de000.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de001.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de002.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de003.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de004.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de005.html

https://oasislandscape.com/cap/atp/Thiem-v-Tsitsipas-liv-Mara-de006.html

http://landrover.gi/land/atp/Thiem-Tsitsipas-im-tv-liv-ccf1.html

http://landrover.gi/land/atp/Thiem-Tsitsipas-im-tv-liv-ccf2.html

http://landrover.gi/land/atp/Thiem-Tsitsipas-im-tv-liv-ccf3.html

http://landrover.gi/land/atp/Thiem-Tsitsipas-im-tv-liv-ccf4.html

http://landrover.gi/land/atp/Thiem-Tsitsipas-im-tv-liv-ccf5.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK01.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK02.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK03.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK04.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK05.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK06.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK07.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK08.html

http://landrover.gi/land/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK09.html

https://oasislandscape.com/cap/atp/Thiem-Tsitsipas-im-tv-liv-ccf1.html

https://oasislandscape.com/cap/atp/Thiem-Tsitsipas-im-tv-liv-ccf2.html

https://oasislandscape.com/cap/atp/Thiem-Tsitsipas-im-tv-liv-ccf3.html

https://oasislandscape.com/cap/atp/Thiem-Tsitsipas-im-tv-liv-ccf4.html

https://oasislandscape.com/cap/atp/Thiem-Tsitsipas-im-tv-liv-ccf5.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK01.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK02.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK03.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK04.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK05.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK06.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK07.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK08.html

https://www.oasislandscape.com/cap/socc/Ss-Slovakia-v-Scotland-liv-hq-tv-UK09.html

https://sv.tie.org/sv/atp/Thiem-Tsitsipas-im-tv-liv-ccf1.html

https://sv.tie.org/sv/atp/Thiem-Tsitsipas-im-tv-liv-ccf2.html

https://sv.tie.org/sv/atp/Thiem-Tsitsipas-im-tv-liv-ccf3.html

https://sv.tie.org/sv/atp/Thiem-Tsitsipas-im-tv-liv-ccf4.html

https://sv.tie.org/sv/atp/Thiem-Tsitsipas-im-tv-liv-ccf5.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin00.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin01.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin02.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin03.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin04.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin05.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin06.html

https://sv.tie.org/sv/socc/Scot-v-Slov-liv-kamran-virgin07.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin00.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin01.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin02.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin03.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin04.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin05.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin06.html

http://landrover.gi/land/socc/Scot-v-Slov-liv-kamran-virgin07.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin00.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin01.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin02.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin03.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin04.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin05.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin06.html

https://www.oasislandscape.com/cap/socc/Scot-v-Slov-liv-kamran-virgin07.html

https://ayf.com/ayf/cll/dr--Video-Slovakia-vs-Scotland-news-90.html

https://ayf.com/ayf/cll/dr--Video-Slovakia-vs-Scotland-news-91.html

https://ayf.com/ayf/cll/dr--Video-Slovakia-vs-Scotland-news-92.html

https://ayf.com/ayf/cll/dr--Video-Slovakia-vs-Scotland-news-93.html

https://ayf.com/ayf/ten/sub-Thiem-v-Tsitsipas-cbn-00.html

https://ayf.com/ayf/ten/sub-Thiem-v-Tsitsipas-cbn-01.html

https://ayf.com/ayf/ten/sub-Thiem-v-Tsitsipas-cbn-02.html

https://ayf.com/ayf/ten/sub-Thiem-v-Tsitsipas-cbn-03.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK01.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK02.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK03.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK04.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK05.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK06.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK07.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK08.html

https://ayf.com/ayf/cll/Ss-Slovakia-v-Scotland-liv-hq-tv-UK09.html

https://ayf.com/ayf/ten/Thiem-Tsitsipas-im-tv-liv-ccf1.html

https://ayf.com/ayf/ten/Thiem-Tsitsipas-im-tv-liv-ccf2.html

https://ayf.com/ayf/ten/Thiem-Tsitsipas-im-tv-liv-ccf3.html

https://ayf.com/ayf/ten/Thiem-Tsitsipas-im-tv-liv-ccf4.html

https://ayf.com/ayf/ten/Thiem-Tsitsipas-im-tv-liv-ccf5.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin00.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin01.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin02.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin03.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin04.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin05.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin06.html

https://ayf.com/ayf/cll/Scot-v-Slov-liv-kamran-virgin07.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de000.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de001.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de002.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de003.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de004.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de005.html

https://ayf.com/ayf/ten/Thiem-v-Tsitsipas-liv-Mara-de006.html

We use URL / Path Parameters to access a resource on the server. For example, a user who can be found by an ID or a tag. Practical examples are platforms on the Internet that have the following URL syntax: domain.com/<username> . Sometimes there is still an @ sign in front of it.

We can implement this simply and elegantly with a URL parameter:

app.get(“/:username”, (req, res) => {

res.send(req.params.username)

})

Everything we enter now after the slash can be processed by Express.js — for example in a database query. For real-world platforms, these are often URLs like twitter.com/louispetrik .

URL parameters are shorter but less readable and, therefore, less suitable to put several of them in a row.

Especially for search engine optimization, we want to keep our URLs slim. And pages that should be displayed in search engines are e.g., the user pages, or product and category pages. Not pages that represent a price-sorted listing of cat food (Nobody googles something like this).

So, URL Parameters so should be used to output a “whole”, static resource.

What is the advantage of one is the disadvantage of the other. Query parameters are longer, more readable because they represent the key in the URL. This makes it easier, e.g. during development — they should rather be used to link to pages that should not be ranked in search engines, often there are several query parameters in one URL — e.g. for result pages, for products. There we need e.g. a keyword and a sorting.

Summing up:

URL parameter for clean, small, search engine friendly URLs that lead to a static resource

for clean, small, search engine friendly URLs that lead to a static resource Query parameters for longer, more readable URLs, which are followed by dynamic content, not relevant to search engines.

Query Parameter in the Amazon Product Search

URL Parameters in Express.js

Requesting URL parameters is not difficult. We identify them by :param_name and can then output them under the name as an element of the request-object.

// localhost/max => output: maxapp.get(“/:username”, (req, res) => {

res.send(req.params.username)

})

We can also have several parameters in a row.

// localhost/max/23 => output: max 23app.get(“/user/:name/:age”, (req, res) => {

res.send(req.params.name + “ “ + req.params.age)

})

Query Parameters in Express.js

Query parameters work almost the same way. But in contrast to URL parameters, we do not define what we expect. All parameters that have been received are available in the req.query object.

// localhost/user?name=max => output: maxapp.get(“/user”, (req, res) => {

res.send(req.query.name)

})

A new parameter is always marked by the question mark.

We can also pass multiple params in the URL. It is important that we separate them with an and sign. But we do not need another question mark.

// localhost/user?name=max&age=23 output: max 23app.get(“/user”, (req, res) => {

res.send(req.query.name + “ “ + req.query.age)

})

Thank you for reading!

Join my Newsletter to stay in touch