how to get URI as parameters in NodeJS - node.js

How can i get the URI and use it as params in nodejs. I am using express.
http://localhost:3000/getParams/param1/param2/param3/paramN
I want to get "/param1/param2/param3/paramN".
This is my current code:
app.get("/getParams/:params", test.params);
Thanks!

You can access the full path as one param, or you can access each segment as a separate param. To get as one param:
app.get('/mysvc/:input(*)', function(req, res)
{
console.log(req.params.input);
// ...
});
Notice the route which says everything (regex match of *) after /mysvc/ will be mapped to the input reques param. Then you can reference it via req.params
In this example, a request to /mysvc/foo/bar will output foo/bar
If you want to get each segment as a separate param then:
app.get('/mysvc/:param1/:param2'
access via req.params.param1, req.params.param2, etc...

In express parameters are available in the req object req.params.parameterName so in your case you can access it within the route like this req.params.params.
The params that you can access from the handler depends upon the route definitions. If you route is like
/my/:one/:two/:three
then you can access them as
req.params.one
req.params.two
req.params.three

Related

Express: req.params vs req.body (JSON)

Which is more correct, and why? Does it depend on the scenario? Is there a standard?
router.post("/user", function(req, res) {
let thisUserId = req.body.userId;
});
router.post("/user/:userId", function(req, res) {
let thisUserId = req.params.userId;
}
Thanks!
This question is more about RESTful API conventions than node or express. Based on generally accepted REST conventions, this is basic CRUD operation:
/* fetch all users */
GET /users
/* fetch specific user */
GET /users/:userId
/* create new user */
POST /users
/* edit specific user */
PUT /users/:userId
/* delete specific user */
DELETE /users/:userId
So in your case I would say req.body is more appropriate, considering you want to create a user.
EDIT: another useful resource that supports this case: 10 best practices for better RESTful API.
req.body is used to access actual form data that you 'posted'.
req.params is used for route parameters, in your case userId which is passed in the parameters:
router.post("/user/:userId", function(req, res) {
let thisUserId = req.params.userId;
}
The official docs:
req.body
Contains key-value pairs of data submitted in the request
body. By default, it is undefined, and is populated when you use
body-parsing middleware such as body-parser and multer.
Link to req.body docs
req.params
This property is an object containing properties mapped to
the named route “parameters”. For example, if you have the route
/user/:name, then the “name” property is available as req.params.name.
This object defaults to {}.
Link to req.params docs
If you want to implement guards or any other logic in your route that relies on that id (of an existing user), you pass the userID in the params.
Let's say you are submitting a form where a new user registers.. You don't want to send the credentials in the parameters since it's confidential data and easily accessible this way. Here it makes sense to put those values in the request-body and use therefore req.body..
As Haris Bouchlis already mentioned in his answer it depends on your CRUD-ops that you like to perform.
In this case I advice to use req.params.userId.
Normal API entries have this standard. To get all the users the path will be '/users'.
To retrieve or update a specific user, the path will be '/users/1'.
API example
Yes there are completely different and used for the different purpose
1.req.params comes from path segments of the URL that match a parameter in the route definition such a /song/:songid. So, with a route using that designation and a URL such as /song/48586, then req.params.songid === "48586".
2.req.body properties come from a form post where the form data (which is submitted in the body contents) has been parsed into properties of the body tag.

Express router order of request executions: `/state/:params` vs `/state/absolute-path`

If I have two REST endpoints:
app.get('/something/:id', ...handlers);
app.get('/something/else', ...handlers);
And I send a request to http://host:port/something/else
Is there a way to make Express router execute the endpoint with absolute path first (/something/else) before executing the one that matches the query params (/something/:id)?
I understand that I can reverse the order of invocation and specify the endpoint with query params last. But logically speaking, absolute path should take priority over query params and I believe that's the default behaviour for Koa.js
Just put the more specific route first and the wildcard route second. Routes are matched in order and the first one that matches handles the request and the others are not then processed. So, put the more specific route for /something/else before the /something/:id and you will see the /something/else route work properly when that's the URL.
// put wildcard route last and more specific route definitions first
// routes are matched in the order they are defined
app.get('/something/else', ...handlers);
app.get('/something/:id', ...handlers);
This does raise the question why you have designed this potential conflict into your URL scheme in the first place. You've essentially overloaded the id namespace and have reserved at least one id value for your own use. This can be managed by careful ordering of the route definitions, but it would generally be better if you didn't have this conflict in your URL design in the first place.
Is there a way to make Express router execute the endpoint with absolute path first ('/something/else') before executing the one that matches the query params ('/something/:id')?
Yes, define the more specific route first.
I understand that I can reverse the order of invocation and specify the endpoint with query params last. But logically speaking, absolute path should take priority over query params and I believe that's the default behaviour for Koa.js
You asked about Express. It matches routes in the order you've defined them. It doesn't try to guess which route it "thinks" you want to match first. It lets you define that exactly via the order of your route definitions.
I don't know Koa.js well, but there is this in the doc for Koa2: Middleware is now always run in the order declared by .use() (or .get(), etc.), which matches Express 4 API.
There are no specific route matching rules for express.js to match the routes.It goes and try to match every registered route with incoming request path and calls route handlers for all matched paths. Thus the following code will work.
app.get('/something/:id', (req, res, next) => {
console.log(`Calling with param ${req.params.id}`);
next(); // if you remove next from here it will not call the rest of the handlers
});
app.get('/something/else', (req, res, next) => {
console.log(`Calling with else`);
next();
});
Output:
Thus the only way to make sure the routes match exactly, define routes in their specific order.
app.get('/something/else', ...handlers);
app.get('/something/:id', ...handlers);

ExpressJS sendFile() cannot send URL GET parameters

I'm new to Node.js and Express.js and its routing. It's all set up correctly and it all works except for the following code.
I tried the following code:
app.get("/game/*", function(req, res) {
res.sendFile(__dirname + "/public/game.html?gameId=" + /\/([^\/]+$)/.exec(req.url)[1]);
});
The goal was to send all requests with /game/{gameId} (where gameId is some number) to /public/game.html?gameId={gameId}.
It correctly gets the requests with /game/, gets the gameId parameter from the URL, and attempts to sendFile() it. However, the sendFile() does not work, saying:
web.1 | Error: ENOENT, stat '/opt/lampp/htdocs/papei/public/game/32'
I've searched this error, and I guess it has to do with a file not being found. The problem is, /public/game.html exists. If I remove the ?gameId... part in the sendFile(), then it works. But I guess the sendFile() is looking for an exact URL, and is not finding it.
Is there any way to send URL GET parameters using ExpressJS?
I think the problem is that sendFile tries to find an exact match (which your query parameters break) as you thought.
You could use express-static to serve the html page, and then redirect to it as needed like so:
app.get("/game/:gameid", function(req, res) {
// Not ideal, as it uses two requests
res.redirect('/game.html?gameId=' + req.params.gameid)
});
Or you could put the html inside a template and render it for the response e.g:
app.get("/game/:gameid", function(req, res) {
// Render the 'game' template and pass in the gameid to the template
res.render('game', {gameid: req.params.gameid})
});
Either way, you don't need to use a catch all route and regex to get query parameters, see req.params or req.query in the express documentation.
Hope this helps.

Express route parsing

I'm trying to create a route for the following URL:
http://localhost:5000/api/querystring?parameter1=value1&parameter2=value2
My route looks like this:
app.get('/api/:querystring/:parameter1?/:parameter2?', function(req, res) {
// do stuff
})
How can I create a route that matches the given URL?
You can't include query string parts in the route ... you will have to leave them off.
app.get('/api/querystring' ...
Then in the callback you can look at req.query to see the parameters. If you were to compare to the query, the order of the query string parameters would matter. It shouldn't.
If you want /api/querystring?parameter1=foo and /api/querystring?parameter2=bar to use different routes, you will have to handle this by calling separate functions within the app.get route callback above.

Node.js : Express app.get with multiple query parameters

I want to query the yelp api, and have the following route:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
When I make a GET request to
http://localhost:3000/yelp?term=food&location=austin,
I get the error
Cannot GET /yelp?term=food&location=austin
What am I doing wrong?
Have you tried calling it like this?
http://localhost:30000/yelp/term/food/location/austin
The URL you need to call usually looks pretty much like the route, you could also change it to:
/yelp/:location/:term
To make it a little prettier:
http://localhost:30000/yelp/austin/food
In the requested url http://localhost:3000/yelp?term=food&location=austin
base url/address is localhost:3000
route used for matching is /yelp
querystring url-encoded data is ?term=food&location=austin i.e. data is everything after ?
Query strings are not considered when peforming these matches, for example "GET /" would match the following route, as would "GET /?name=tobi".
So you should either :
use app.get("/yelp") and extract the term and location from req.query like req.query.term
use app.get("/yelp/term/:term/location/:location") but modify the url accordingly as luto described.
I want to add to #luto's answer. There is no need to define query string parameters in the route. For instance the route /a will handle the request for /a?q=value.
The url parameters is a shortcut to define all the matches for a pattern of route so the route /a/:b will match
/a/b
/a/c
/a/anything
it wont match
/a/b/something or /a
Express 4.18.1 update:
using app.get("/yelp/term/:term/location/:location"), your query string can be yelp/term/food/location/austin
So your request url will look like this:
http://localhost:3000/yelp/term/food/location/austin

Resources