express.js route with or without a parameter - node.js

I use node.js v8.11.1 and express 4.16.3.
Say I have the following route
app.get('/:id', function(req, res){
I want to do something like
if(req.params.id) then query1
else //no id param in the url
query2
So, I could go either to http://localhost:3000/ or to http://localhost:3000/504 and the routes will respond accordingly.
but when I go to http://localhost:3000/ I just get Cannot GET /
How do I fix my routes?
Thanks

Make your route parameter optional using ? operator.
Change your route with following:
app.get('/:id?', function(req, res){
Now it should work for both: http://localhost:3000/ or http://localhost:3000/504

I agree with #n32303, you can do:
app.get('/', function(req, res){
//Called when there is no id specified
}
app.get('/:id', function(req, res){
// Called when an Id is specified (req.params.id will be set )
}
To eliminate the need for an if statement

Related

nodejs get req.param from route

I am trying to get the url parameter of an url request from the front in my nodejs backend.
fetch(`http://localhost:9000/sent/5768797675645657`)
Here is my app.js file :
app.use('/sent/:id', require('./routes/sent'));
And my /routes/sent.js file :
router.get('/', function(req, res) {
console.log(req.params)
})
How can I get the console.log(req.params) to work?
So, app.use accepts two parameters: the portion of the URL, and a callback function.
In order to your app works, you've to change the /routes/sent.js and make it exports the function, so it can be used when you're requiring it.
Just change it to
module.exports = function (req, res) {
console.log(req.params)
}
and you're ready to go!

first portion of route url is not included

I have a route where I built two GET APIs. I would like one to redirect from /download to /zip all while passing a parameter. The problem is I am getting a 404 for some reason the routes url is not being included in the redirect()
Here are the APIs.
// respond with xml from from static folder
router.get('/zip/:id', function (req, res) {
fileName = req.params.id
});
router.get('/download', function (req, res, next) {
var id = req.query.id
res.redirect('/zip?id='+ id);
});
module.exports = router;
I get a 404 when testing the URL:
localhost:8000/rest/pluto/v1/plugin/download?id=networktool
I am thinking it might be how I have the middleware setup but not real sure. I'm still new to node/express.
You are redirecting to a route that isn't actually defined. With your /zip/:id route definition:
router.get('/zip/:id', function (req, res) {
var fileName = req.params.id
});
The way that is defined, you have to have id information in the URL itself, so while the following routes would work:
/zip/networktool
/zip/1234
these routes would not:
/zip
/zip?id=networktool
/zip?id=1234
because Express is looking for the id to be built into the route itself. So you can do one of two things. You can either change your redirect to:
router.get('/download', function (req, res, next) {
res.redirect('/zip/'+ req.query.id);
});
or, you can modify your /zip route to make the id parameter optional with ?:
router.get('/zip/:id?', function (req, res) {
var fileName = (req.params.id) ? req.params.id : req.query.id;
});
I would recommend the first option, as the latter optional parameter only makes your zip route more complicated and require extra handling of whether id is actually passed to your route.
The path /zip/:id is expecting a path parameter not a query parameter.
You should redirect like this
res.redirect('/zip/'+ id);

How to get the router values containing special characters in express router

I am working on to get route value in Nodes js using express framework
the url goes like
http://localhost:3000/course/view/turbine/turcV39/%20V42/%20V44/%20V47
Need to get the value "V39/%20V42/%20V44/%20V47" from the above url and route
router.get('/view/turbine/:turc?', function(req, res) {
console.log('a');
});
You can use regex to get it, like this
app.get(/\/view\/turbine\/turc(.*)/, function(req, res) {
console.log(req.params[0])
});

ExpressJS : Modify route in a middleware

Running an express app which for example has a route like /home and others. Now I want to change url to /en/home, /sp/home etc.
My requirement is to map these routes from /en/home to /home and similarly /sp/home to /home and add a header on request object on basis of /en and /sp
I want to write a middleware which will modify routes and extract information from it on basis of route as mentioned above.
I can use regex to do the second part but could not find a way to modify route on the fly.
You can try below middleware:
app.use('/en/home', function(req, res, next) {
req.headers.lang = 'en';
res.redirect('/home');
});
app.use('/sp/home', function(req, res, next) {
req.headers.lang = 'sp';
res.redirect('/home');
});

Use a route as an alias for another route in Express.js

I'm working on a basic blog in Express.js. Say I have route structure like this:
/blog/page/:page
I would also like a /blog route that is essentially an alias for /blog/page/1. How can I handle this easily in Express?
All routes are defined like such:
app.get('/path', function(req, res) {
//logic
});
Use res.redirect to tell the browser to redirect to /blog/page/1:
app.get('/blog', function(req, res) {
res.redirect('/blog/page/1');
});
app.get('/blog/page/:page', function(req, res) {
//logic
});
Use a shared route handler and default to page 1 if the page param is not passed:
function blogPageHandler(req, res) {
var page = req.params.page || 1;
//logic
}
// Define separate routes
app.get('/blog/page/:page', blogPageHandler);
app.get('/', blogPage);
// or combined, by passing an array
app.get(['/', '/blog/page/:page'], blogPageHandler);
// or using optional regex matching (this is not recommended)
app.get('/:_(blog/)?:_(page/)?:page([0-9]+)?', blogPageHandler);

Resources