当通配符匹配时,节点快速 GET 路由每隔一个请求失败

bre*_*cj7 1 javascript regex node.js express

我有一个路由器设置来接受任何与其中的“API”一词不匹配的 GET 请求。这是针对我的单页应用程序,因此我的 javascript 无法使用其路由的任何书签网址。

问题是每次我使用测试 url 刷新页面时都会失败。

网址:http://localhost:3000/my-test-url

第一次点击 url,我的索引页面加载。

第二次刷新页面,错误。

无法获取 /my-test-url

第三次刷新它有效,第四次失败,依此类推。

app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.set('views', path.resolve(__dirname, '../client'));

// serve static content for the app from the "client" directory in the     application directory
//express.static(path.join(__dirname, '../client')))
app.get(/^(?!.*\bapi\b).*$/ig, function(req, res) {
    res.render('index');
});

app.listen(app.get('port'), function() {
    console.log('Server up: http://localhost:' + app.get('port'));
});
Run Code Online (Sandbox Code Playgroud)

有没有什么东西可以在第一次工作后使请求保持打开状态?浏览器中的网络选项卡每次都显示相同的 URL,但交替显示 404 错误。任何建议表示赞赏。

更新,似乎有问题可能不是正则表达式,而是路由器每次如何解释它。当我设置这样的路线时:

app.get('*', function(req, res) {
    console.log(req.route);
    res.render('index');
});
Run Code Online (Sandbox Code Playgroud)

正如我所料,它似乎每次都有效。

t.n*_*ese 5

问题似乎是g标志。如果只是使用/^(?!.*\bapi\b).*$/i 问题应该就没有了。

但我实际上不知道这种行为是错误还是为什么会发生。