gpt4 book ai didi

angularjs - Angular - Node - 可以告诉 Node 哪个 block 来自路由请求

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:43 27 4
gpt4 key购买 nike

是否可以向我的 Node 应用程序 req 发出请求:

app.get('/*', function(req, res) {
res.sendFile(__dirname + '/site/index.html')
});

它来 self 的 Angular 路由的哪个 block (即我希望能够识别 otherwise block 中的内容);

.when('/user',
{...}
)
.when('/login',
{...}
)
.otherwise(
{...}
)

我想要它的原因是因为我想做类似的事情:

app.get('/*', function(req, res) {
if (req is from otherwise block) {
res.status(400).sendFile(__dirname + '/site/index.html')
} else {
res.sendFile(__dirname + '/site/index.html')
}
});

我唯一能想到的就是创建一个包含所有允许路由的数组,但我不喜欢重复:p

最佳答案

由于 Angular 和 Node 之间的唯一链接是它发出的 HTTP 请求,因此您所能做的就是检索 url 并分析它来自哪个路由。

我会在 Node 中定义您所了解的路由,然后针对 404 错误进行回退:

app.get(['/login', '/user'], function(req, res) {
res.sendFile(__dirname + '/site/index.html')
});

app.get('*', function(req, res) {
res.status(404).sendFile(__dirname + '/site/404.html')
});

关于angularjs - Angular - Node - 可以告诉 Node 哪个 block 来自路由请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41942968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com