I'm trying to use other URL to be used as a data in my app.
For example, If I visit localhost:3000/https://www.google.com/robots.txt
Then I would like to get https://www.google.com/robots.txt as a parameter so I can use it.
I tried the following approach but it only works if the trailing value has no slash.
app.get('/:id', function (req, res) {
res.send(req.params)
})
Is there a possible way to get the appended URL?
You can use /* to grab the Parameters and then get the index 0 to get the exact URL
app.get('/*', function(req, res) {
var url = req.params[0];
res.send(url);
});
Related
I'm attempting to make a request to an API to get a users' avatar. This is the code I have, however it's returning a "player is not defined" error.
app.get(`/api/avatar/${player}`, function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${player}/avatar`);
});
Essentially, if the URL is /api/avatar/3925 it would send them to http://cdn.simulping.com/v1/users/3925/avatar.
Assuming you're using Express.JS for the routing, the correct way for doing that is:
app.get('/api/avatar/:id', function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${req.params.id}/avatar`);
})
Assuming you are using Express routing, then you would need to utilise route parameters.
The Express documentation provides:
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.
In this case:
Route path: /api/avatar/:player
Request URL: http://localhost:3000/api/avatar/3925
req.params: { "player": "3925" }
The working code:
app.get('/api/avatar/:player', function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${req.params.player}/avatar`);
});
You can read more about it in the Route Parameters section of the Express Routing Guide.
Currently I have this route:
router.get('/:board/:threadId', function(req, res, next) {
// doing stuff
});
So the users go to /a/1 and it triggers this route with board = a and threadId = 1.
What I want now is that the users need to go to /a/1.html to trigger this route (but threadId should still equal 1. How do I add the .html in that route? I tried /:board/:threadId.*.html$ after reading the express documentation but it's not working as expected.
The hyphen (-) and the dot (.) are interpreted literally by string-based paths.
Did you try this?
router.get('/:board/:threadId.html', function(req, res, next) {
// doing stuff
});
How does the framework know which api is been called?
app.get('/user/:userId/name/export', function (req, res) {
var userId = req.params.userId;
}
app.get('/user/:userId/name/:name', function (req, res) {
var userId = req.params.userId;
var name = req.params.name
}
I'm working on an api gateway, need some customization of access control. It need to block the api call and check the roles of user through the params in path, like userId and name in db. If matched the config in file, will pass the acl and call the api, otherwise, will return 401. So if the url pattern is similar, I found it's hard to distinguish two api which is exactly been call. Any suggestions? Really appreciate for you help!
Express router calls each callback, which match the URL path.
Route /user/123/name/admin matches only the second path, but route /user/123/name/export matches both of them.
If you end the request on the first callback, then the second will never be called:
app.get('/user/:userId/name/export', function (req, res) {
var userId = req.params.userId;
res.end();
}
app.get('/user/:userId/name/:name', function (req, res) {
var userId = req.params.userId;
var name = req.params.name
}
Callbacks will be called according to adding sequence. So the global paths, like app.get('*', ...) must be added at the very end.
I've found in quite a few SO posts that in order to rewrite a URL in Express 4 I would do something like the following:
router.use('/one/:someId', (req, res, next) => {
req.url = `/two/${req.params.someId}`;
next();
});
router.get('/one/:someId', (req, res) => {
res.send("reached /one/:someId");
});
router.get('/two/:someId', (req, res) => {
res.send("reached /two/:someId");
});
But when I try this, not only does the URL does not change to my expected "/two/some integer" and stays being "/one/some integer" but it gets to the 404 - Not Found page I have set up in my app file.
This routes are in a router file and I have also tried setting the URL to:
req.url = `/routerPath/two/${req.params.someId}`;
but the result is exactly the same.
So what could I be missing?
Thank you.
You have to distinguish two kinds of redirects:
Internal redirects work on the server, without the client noticing. They are a convenience for your server programming and never necessary - you could always introduce a helper method that gets called by all endpoints.
HTTP redirects advise the client (e.g. a web browser) to go to a different URL. Since you expect the URL to change, that's the one you want.
Simply call res.redirect, making sure to encode special characters:
router.get('/one/:someId', (req, res) => {
res.redirect(`/two/${encodeURIComponent(req.params.someId)}`);
});
router.get('/two/:someId', (req, res) => {
res.render("reached /two/:someId");
});
I'm trying to define a generic handler for express.js routes
the idea is to get something like
get /api/xxx/yyy -> get all data
get /api/xxx/yyy/11-> get record 11
I need to capture xxx/yyy on one variable and 11 in another
this works fine:
app.get('/api/*', function(req, res, next) {
I'm not capturing anything, but I read it with the req.path property
But I can't seem to solve this:
app.get('/api/*/:id(\\d+)$', function(req, res, next) {
If I try with:
http://localhost:3000/api/clientes/2
this is what I get in req.params:
req.params = [ '2', id: 'clientes' ]
It seems like the path get binded to id, and the id is unbound to any variable.
moreover, if I try fetching this:
http://localhost:3000/api/clientes/nuevos/2
The route doesn't match
So, I'm looking for a regexp that allow me to catch several paths (xxx/yyy) and also the last one if it's a number (11)
I could solve it using a regular expression instead of a string:
app.get(/\/api\/(.*)\/(\d+)/, function(req, res, next) {
Now if I try with
http://localhost:3000/api/clientes/menores/71
I get
req.params = [ 'clientes/menores', '71' ]
But I'd like to know if there's some way to use the :id marker...