Express 指定路由的默认行为
我正在创建一个带有express、next 和ace (atlassian-connect-express) 的应用程序。
Express + next 要求我使用这样的路由:
app.all('*', (req, res) => {
return handle(req, res)
})
但我需要 atlassian-connect.json 的特殊行为。在 ace 示例中,它是这样完成的:
app.get('/', (_, res) => {
res.redirect('/atlassian-connect.json');
})
问题是第一条路由仍将用于 /atlassian-connect.json,所以我也必须覆盖它。
但是如果我像这样手动尝试,我会遇到以下问题:
app.get('/atlassian-connect.json', (_, res) => {
let content = fs.readFileSync(path.resolve('atlassian-connect.json'));
let json = JSON.parse(content);
res.send(json)
})
json 包含这样的部分:
"baseUrl": "{{localBaseUrl}}"
如果我没有 /atlassian-connect.json 的手动路由,则 localBaseUrl 会自动填写。当我使用我的路线时,它不会填写。我该如何填写?
I am creating an app with express, next and ace (atlassian-connect-express).
Express + next requires me to use routes like this:
app.all('*', (req, res) => {
return handle(req, res)
})
But I need special behavior for an atlassian-connect.json. In the ace example, it is done like this:
app.get('/', (_, res) => {
res.redirect('/atlassian-connect.json');
})
The problem with that is that the first route will still be used for /atlassian-connect.json, so I have to overwrite that as well.
But if I try it manually like this, I run into the following problem:
app.get('/atlassian-connect.json', (_, res) => {
let content = fs.readFileSync(path.resolve('atlassian-connect.json'));
let json = JSON.parse(content);
res.send(json)
})
The json contains a part like this:
"baseUrl": "{{localBaseUrl}}"
And the localBaseUrl gets automatically filled out if I dont have a manual route for /atlassian-connect.json. When I do use my route, it doesn't fill it out. How can I fill it out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现发送填写的 json 并不是 Express 的默认操作,而是 ace 模块所做的操作。
所以我发现的方法就是像这样手动定义所有路由:
我尝试使用正则表达式来匹配除 atlassian-connect.json 之外的所有路由,并得出这样的结果:
^(?!atlassian-connect.json json$).*$
但它似乎不起作用..所以我想最简单的方法就是手动声明所有路由。编辑:
我想出了如何让正则表达式工作(愚蠢的我不阅读文档)
固定路由:
What I found out that this sending the filled out json is not something default from express but rather something the ace module does.
So the way I found is to just define all the routes manually like this:
I tried using regex to match on all the routes except the atlassian-connect.json and came up with this:
^(?!atlassian-connect.json$).*$
but it doesn't seem to work.. So I guess the easiest way is just to declare all the routes manually.EDIT:
I figured out how to get the regex to work (silly me not reading the docs)
Fixed routing: