Direct users to the same page with two different paths (Javascript/ nodejs)
I am writing a code that will display a HTML file when the path /contact or /contact-us is entered into the URL. The problem that I am having is that I want to put both of them into the same function rather than have more than one function to do essentially the same thing. I tried finding a solution or help online but I couldn't. What I have tried is to use the or comparison operator for the function to determine the differences between the two paths but that didn't work. Is there a way that I can do this?
This is my code:
app.get('/contact' || '/contact-us', (req, res)=> {
let path = require('path');
res.sendFile(path.join(__dirname, 'public', 'index.html'));
//the or comparison operator does not work and Im not sure if there's another way to do this
});
Solution 1:
By putting your list of paths into an array, you can use multiple paths, as explained in this answer. Here is an example for your problem:
app.get(['/contact','/contact-us'], (req, res)=> {
let path = require('path');
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});