expressjs pattern to match the rest of the path - node.js

I'm trying to create an endpoint that contains an actual path that I extract and use as a parameter. For instance, in the following path:
/myapi/function/this/is/the/path
I want to match "/myapi/function/" to my function, and pass the parameter "this/is/the/path" as the parameter to that function.
If I try this it obviously doesn't work because it only matches the first element of the path:
app.get("/myapi/function/:mypath")
If I try this it works, but it doesn't show up in req.params, I instead have to parse req.path which is messy because the logic has to know about the whole path, not just the parameter:
app.get("/myapi/function/*")
In addition, the use of wildcard routing seems to be discouraged as bad practice. I'm not sure I understand what alternative the linked article is trying to suggest, and I'm not using the query as part of a database call nor am I uploading any information.
What's the proper way to do this?

You can use wildcard
app.get("/myapi/function/*")
And then get your path
req.params[0]
// Example
//
// For the route "/myapi/function/this/is/my/path"
// You will get output "this/is/my/path"

Related

How do Express router.param and router.route work in terms of defining URL parameter names?

I created a router file using Express. The callback functions reside in their discrete "controllers" files. Following is an excerpt of the parts relevant to my question, and lines such as require of controller functions have been omitted:
const express = require('express');
const router = express.Router();
// This should run first
router.param('coolParamName', validateParamBeforeHandlingReqs);
// Param name is ↑↑↑↑↑ "consumed" here, although NOT defined yet
// This should run after the above code
router.route('/').get(getAllUserNames).post(createUser);
router.route('/:coolParamName').get(getUserName).patch(updateUser).delete(deleteUser);
// Param name is ↑↑↑↑↑ defined here, and was consumed earlier - how?
As the comments explain, it seems like the param name has been defined as coolParamName on the bottom, but "consumed" by the code written above it. This feels strange to me, because I feel it's natural to define first and then use later - is it just me? Did I write a code that's against the intended design pattern?
I would like to understand how Express defines the name of param, and how router.param and router.router handle them.
router.param('coolParamName') essentially registers a callback that will get called for any route (in that router) that uses the :coolParamName parameter and matches the current request. The callback will get called once per request BEFORE the route that matches the request that contains the :coolParamName parameter.
It's kind of like middleware for a matching parameter. It allows you to automatically configure some setup code anytime that particular parameter is matched in a route.
FYI, I expect that router.param() may be one of the least used features of Express since it can be accomplished many other ways, but it probably works best for validation-type code that checks named properties for validity before the route itself gets called.
You could accomplish the same thing by just using a piece of middleware on that specific route too or even just calling a function inside the route handler. So, this is just a nicety feature if you happen to use the same parameter in multiple routes.

Can I use wildcard pattern matching in Libsoup server?

I am using libsoup to implement a HTTP server. I want to catch all wildcard patterns of the form -
"/foo/*/bar/"
in the URL but I dont know how to do this right now.
How can I implement this using the Libsoup and GLib libraries?
My current code is using:
soup_server_add_handler (server, "/foo/*/bar/", NULL, server_callback,
unregister_callback, data);
The above doesnt work if I try to catch the URL "/foo/abc/bar"
Please let me know if this is possible in libsoup and what is the correct syntax to be passed to soup_server_add_handler()
soup_server_add_handler() doesn't take wildcards in its path. You will have to add a handler for / and then examine the path handled to see if it matches your wildcard expression.
There is a merge request that adds something like this to Soup, but it is stalled.

router.get(), use only part of url?

Currently I have this code:
router.get('/admins', function(res,req) {
However, I want it to be when someone goes to say, 'localhost:5000/admins/54323', I want the node-js app to notice 'hey, they're requesting for an admins list! lets find it'. However, with the router.get() function it only works if it is exactly that, is there a way to have it so if only the start is /admins then it sets a variable for the final part?
Through more research, I found an answer. I just put /:id after the /admins bit,
you can access the rest of the string in req.params.id where .id is the same as the string written after the : sign

get method with colon and question mark in server path

Follow up question: What is the code "res.json(false);" doing? Doesn't that print out false on the page instead of showing the data I want?
I'm looking at the following sample code. I understand that .get( is the method and /:characters? is the server path. In this search, what is the point of the colon and the question mark in the path? Shouldn't the question mark come before characters because it is a query?
app.get('/:characters?', function (req, res) {
var chosen = req.params.characters;
if (chosen) {
console.log(chosen);
for (var i = 0; i < characters.length; i++) {
if (chosen === characters[i].routeName) {
res.json(characters[i]);
return;
}
}
res.json(false);
} else {
res.json(characters);
}
});
In this case the question mark signifies an optional parameter "characters". This allows for an endpoint that MAY have a value or not. They are then testing against that parameter to see if it was included. If so they will iterate through the "characters" object and return any matching entry to the endpoint the user specified.
Simplest answer:
:XXX means that its a URL parameter. (i.e. req.params.XXX will pick up what the XXX is)
? means that the parameter is optional. (i.e. the client-side user doesn't need to include this parameter in the url).
So:
/:characters? would allow for: both / AND /yoda to hit this route.
:characters isn't actually part of the query string. It will be part of the url.
The url will be something similar to the following (assuming you are running this server locally and on port 8080):
http://localhost:8080/abcdefg
And in that case, req.params.characters will be 'abcdefg'
Putting an explicit question mark in the route definition is a mistake, in my opinion. I'm not entirely sure what purpose that question mark would serve.
For the follow up question, what it appears to be doing is looking for a match in the characters variable (which I assume is defined externally) by characters[i].routeName, and returning the found value. If no value is found, that's when it sends back false (or tries to - to be honest, I'm not sure what express will do if you try using res.json(false), since I'm not sure false is valid JSON).
This is a normal case that a endpoint or a resource can contain a variable that means
lets take facebook for example so if you see the timeline of MarkZukerberg then the endpoint or url is
https://www.facebook.com/zuck
if you see your own profile then that zuck will be replaced by your name. so the name parameter here is a variable.
When ever a variable is a part of the url then we precede that with a colon sign in express syntax
and if we want to send some other values as the query parameter then we use ? mark to tell the server that the following string will be a query parameter
?name=value&age=12&gender=male
And
res.json(false)
will just return false as the response nothing else

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