Hiufan
Hiufan

Reputation: 57

express.js share routes to render page in different way

I have written a page called sponsorCenter, but I need to control it with two routes:

app.get('/sponsorCenter',function(req, res){});

app.get('/sponsorCenter/all',function(req, res){});

The header, footer and the right columns are same. When I change the URL, only the left column will change.

So my question is can I use just one route to judge the different access and render the page? Because only the left column is different, so I think it is not necessary to render the other parts via another route.

Upvotes: 0

Views: 106

Answers (1)

Rahul Nanwani
Rahul Nanwani

Reputation: 1277

Yes you can accept the route parameter as a variable. The same variable will be accessible to the controller in the request req parameter.

app.get('sponsorCenter/:type*?', function(req, res) {
    console.log(req.params.type);
});

This would match all routes like sponsorCenter, sponsorCenter/all, sponsorCenter/admin, sponsorCenter/user1 etc.

Upvotes: 1

Related Questions