路线未正确定向 [英] Route not being directed correctly
问题描述
我已将Express设置为使用以下路径:
I have set up express to use the following paths:
const profile = require("./api/profile")
const events = require("./api/events")
app.use("/api/events", events)
app.use("/api/profile", profile)
在事件和配置文件 index.js
文件中,我具有以下内容:
Inside the events and profile index.js
files I have the following:
const router = require('./../../modules/router.js')
router.get('/', (req, res) => {
})
module.exports = router
我的router.js文件:
My router.js file:
const express = require("express")
const cookieParser = require('cookie-parser')()
const cors = require('cors')({origin: true})
const router = express.Router()
const firebase = require("./firebase.js")
// https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint
// Must have header 'Authorization: Bearer <Firebase ID Token>'
const validateFirebaseIdToken = (req, res, next) => {
if ((!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) &&
!req.cookies.__session) {
res.status(403).send({ "error": 'Unauthorized'})
return
}
let idToken
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
// Read the ID Token from the Authorization header.
idToken = req.headers.authorization.split('Bearer ')[1]
} else {
// Read the ID Token from cookie.
idToken = req.cookies.__session
}
firebase.admin.auth().verifyIdToken(idToken).then((decodedIdToken) => {
req.user = decodedIdToken
return next()
}).catch(error => {
res.status(403).send({"error": 'Unauthorized'})
})
}
router.use(cors)
router.use(cookieParser)
router.use(validateFirebaseIdToken)
module.exports = router
由于某种原因,路由器在我每次调用它们时都会混淆路径/api/events/
和/api/profile/
.对于所有其他路径,它都可以正常工作.我该如何阻止这种情况的发生?
For some reason, the router mixes up the paths /api/events/
and /api/profile/
whenever I call them. For all other paths it works fine. How can I stop this from happening?
推荐答案
如果您将同一路由器用于事件和配置文件,则可能是问题的根源.
If you are using the same router for both events and profile, it could be the source of your issue.
您是否已经测试过为每个模块创建一个路由器?
Have you tested to create one router for each module?
也许可以在事件和个人资料上尝试以下方法:
Maybe try something like this for both events and profile:
const router = require('express').Router()
router.get('/', (req, res) => {
})
module.exports = router
这篇关于路线未正确定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!