Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access URL segment(s) in ExpressJS

So let's say we have the following url:

http://example.com/shops/map/search

I want to access the second segment (map) and check its value. How can I achieve this in Express? Thanks in advance!

like image 398
Codearts Avatar asked Nov 28 '22 06:11

Codearts


1 Answers

you have to configure your express routes to accept url segments.

app.get('/shops/:type/search', function (req, res) {
    res.send(req.params)
})

For a request like this http://example.com/shops/map/search

req.params will contain required URL segment.

Request URL: http://example.com/shops/map/search
req.params: { "type": "map" }
like image 180
Ratan Kumar Avatar answered Dec 05 '22 12:12

Ratan Kumar