How to render the same page from different get request?

2.5k Views Asked by At

So I'm using express and I was wondering if there was a way to use an array or chain the get request url for my routes. I been searching though documentation and came short. I figured I'd ask here before assuming it's not possible.

Currently this is how I deal with multiple routes that will render the same page.

app.get('/', function (req, res) {
    res.render('home', {
        title: 'Home Page Title'
    });
}); 

app.get('/index.html', function (req, res) {
    res.render('home', {
        title: 'Home Page Title'
    });
});
app.get('/home', function (req, res) {
    res.render('home', {
        title: 'Home Page Title'
    });
});

As you can see, they all render the same page and title. It greatly increases my file size and makes it unfriendly to look though. Plus if I need to change how the page renders at the express routes level, I have to update multiple times.

Thanks for taking your time to help!

1

There are 1 best solutions below

1
Boklucius On BEST ANSWER

What about this:

var homeFunc = function (req, res) {
    res.render('home', {
        title: 'Home Page Title'
    });
}
app.get('/', homeFunc);
app.get('/index.html',homeFunc);
app.get('/home', homeFunc);