【问题标题】:Regex for URL path match not working. How to allow certain routes? [duplicate]URL 路径匹配的正则表达式不起作用。如何允许某些路线? [复制]
【发布时间】:2020-03-12 22:37:26
【问题描述】:

我在我的路由器中使用这个正则表达式

/^\/(?!api|mailAction(\/|\?))(.*)$/

因为路由器只允许api/或api?或mailAction/或mailAction?

现在路由器设置如下 -

app.get(/^\/(?!api|mailAction(\/|\?))(.*)$/, (req, res) => {
            console.log('not matched')
            if (req.isMobile) {
                return res.sendFile(path.join(FE.APP_PATH, "dist", "mob", this.clientName, "index.html"));
            } else {
                return res.sendFile(path.join(FE.APP_PATH, "dist", "web", this.clientName, "index.html"));
            }
        });

当 URL 为 http://fe.localhost:4000/mailAction?t=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXfVCJ9.eyJ0b2tlbklkIjo2MywiaWF0IjoxNTg0MDE4NjE2LCJleHAiOjE1ODQwNTQ2MTZ9.mCKwrMN2ppLlEMSp4U1_AwmNLDOjplOmnYTaFADf6FQ&a=ACT00000205 时,它仍会以中间件的形式提供 index.html 文件。我无法理解为什么。请帮忙。

提前致谢。

【问题讨论】:

    标签: javascript node.js regex express


    【解决方案1】:

    您是否尝试过这样(没有负前瞻)?

    ^\/(api|mailAction)(.*)$

    【讨论】:

    • 我只想在 index.html 与 api 或 mailAction 不匹配时提供它。这就是我使用负前瞻的原因。
    • (?!^\/(api|mailAction)(.*)$) 呢?
    猜你喜欢
    • 2014-08-23
    • 2016-02-09
    • 2022-08-11
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2018-05-19
    • 2023-01-31
    • 1970-01-01
    相关资源
    最近更新 更多