将用户定向到具有两个不同路径的同一页面 (Javascript/nodejs)
Direct users to the same page with two different paths (Javascript/ nodejs)
我正在编写一个代码,当在 URL 中输入路径 /contact 或 /contact-us 时,它将显示一个 HTML 文件。我遇到的问题是我想将它们都放入同一个函数中,而不是让多个函数来做本质上相同的事情。我尝试在网上寻找解决方案或帮助,但我找不到。我尝试过对函数使用 or 比较运算符来确定两条路径之间的差异,但这没有用。我有办法做到这一点吗?
这是我的代码:
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
});
通过将路径列表放入数组中,您可以使用多个路径,如 this answer 中所述。这是您的问题的示例:
app.get(['/contact','/contact-us'], (req, res)=> {
let path = require('path');
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
我正在编写一个代码,当在 URL 中输入路径 /contact 或 /contact-us 时,它将显示一个 HTML 文件。我遇到的问题是我想将它们都放入同一个函数中,而不是让多个函数来做本质上相同的事情。我尝试在网上寻找解决方案或帮助,但我找不到。我尝试过对函数使用 or 比较运算符来确定两条路径之间的差异,但这没有用。我有办法做到这一点吗?
这是我的代码:
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
});
通过将路径列表放入数组中,您可以使用多个路径,如 this answer 中所述。这是您的问题的示例:
app.get(['/contact','/contact-us'], (req, res)=> {
let path = require('path');
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});