[英]Node Express.js set route as a variable
我想知道是否有辦法做到這一點,或者是否可以做到這一點:
在express.js中,我創建了一條路由:
app.get('/hello', (req, res) => {
// Do stuff here
});
關於作品,但是我想知道是否可以添加變量:
app.get('/' + myVariable, (req, res) => {
// so then I can do with the res of myVariable:
if (res = 'something') {
// do something
} else if (res = 'somethingelse') {
// do something else
}
//or use SWITCH CASE ...
});
是否有辦法做這樣的事情?
這可以通過Route paramerers完成。
app.get('/:myVariable', (req, res) => {
const myVariable = req.params.myVariable;
if (myVariable = 'something') {
// do something
} else if (myVariable = 'somethingelse') {
// do something else
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.