【问题标题】:Make dynamic path in node and express js在节点中制作动态路径并表达js
【发布时间】:2016-07-28 13:16:05
【问题描述】:

我正在尝试使路线动态像 /review-{title}-{id} ,但导致错误不知道为什么,如果用户输入错误的参数而不是如何处理。 我的客户要求如上所述,我不擅长节点并表达请任何人建议如何制作上述路线。 另外,如果我需要像这样 /review/:title/:id 格式制作路线,我该怎么做。

我正在尝试,但它会将我重定向到 404 页面,

请在里面找到我现有的代码详细信息, server.js

this is working.. 
app.get('/review', (req, res) => {
 res.sendFile(path.resolve(__dirname, '../client/review.html'));
});

but not this one..
app.get('/review-*-*', (req, res) => {
 res.sendFile(path.resolve(__dirname, '../client/review.html'));
});

Also not this one working
app.get('/review/*/*', (req, res) => {
 res.sendFile(path.resolve(__dirname, '../client/review.html'));
});

This is 404 page which call evrytime while accessing dynamic pages
app.get('/*', (req, res) => {
 res.sendFile(path.resolve(__dirname, '../client/404.html'));
});

【问题讨论】:

标签: node.js express routes js-routes


【解决方案1】:

查看Express 中路由的语法。

在大多数情况下,最好使用路由参数,例如:

app.get('/review/:title/:id', (req, res) => {
 res.sendFile(path.resolve(__dirname, '../client/review.html'));
});

更多的灵活性(但对大多数开发人员来说更不透明)将是匹配正则表达式。

我认为你可以在单词中间放一个 *(他们举了一个像 '/abc*def' 这样的例子,但我不确定它与你正在做的其他事情有多好,我不知道'如果你这样做,你不会认为你可以在模式中有多个 *。)

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 2016-07-09
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多