node.js - 如何在快递中处理角度路线,得到:“res.sendFile 不是函数”
问题描述
我有一个从节点服务器作为静态文件提供的角度应用程序。如果我从http://localhost:3000开始,一切正常,所有角度路由都有效。但是,如果我直接输入带有角度路由的地址(例如:http://localhost:3000/login)或者如果我刷新页面,我会收到错误消息:“TypeError: res.sendFile is not a function” .
我已经在这里搜索堆栈溢出,但我找不到解决这个问题的可靠答案。
这是 app.js 的路由部分:
app.use("/api", commonRoutes);
app.use("/api/auth", authRoutes);
// This returns the static file, but only for http://localhost:3000
app.use((res, req, next) => {
res.sendFile(path.join(__dirname, "angular", "index.html"));
});
解决方案
你的app.use((res, req, next)
论点顺序不正确。尝试
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "angular", "index.html"));
});