【发布时间】:2020-04-15 15:23:16
【问题描述】:
由于提供自定义 OPTIONS 方法,我有自定义 cors 设置。下面的代码
// region cors
// app.use(cors()); // - fixme > using cors lib will disable http options for all routes. avoid using it.
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Authorization");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, HEAD, OPTIONS")
next();
});
// endregion cors
这里有一些路线。行为怪异。
const router = express.Router();
router.options(`/`, authMiddleware, options);
router.get(`/term`, authMiddleware, search);
router.get(`/`, authMiddleware, search);
router.get(`/recent`, authMiddleware, getRecentSearchHistory);
export {router}
你可以看到
~/term 和 / 是相同的。我还制作了~/term,以检查它是否正常工作。
问题是,对/的api请求会被阻止!!?
出于某种原因,/term 工作得很好,但 express 只会阻止 / 根路由。
(实际的绝对路径是http://localhost:3000/api/search)
express 有没有专门为“搜索”或其他东西预留的路线名称?
我不明白这种行为。
【问题讨论】: