我已将Express设置为使用以下路径:
const profile = require("./api/profile")
const events = require("./api/events")
app.use("/api/events", events)
app.use("/api/profile", profile)
在事件和个人资料index.js
文件中,我有以下内容:
const router = require('./../../modules/router.js')
router.get('/', (req, res) => {
})
module.exports = router
我的router.js文件:
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/
。对于所有其他路径,它工作正常。我怎样才能阻止这种情况发生?
答案 0 :(得分:0)
如果您对事件和个人资料使用相同的路由器,则可能是您的问题的根源。
您是否测试过为每个模块创建一个路由器?
也许为这些事件和个人资料尝试类似的事情:
<div role="button" class="collapsable" aria-describedby="xyz" tabindex="0" title="collapse">
<img src="/images/icons/dash.png" alt="collapse" title="collapse" class="opaque pull-right"/>
</div>