小编Fab*_*hon的帖子

Angular + Node + Express:sendFile 方法出现意外令牌

我有点被困在这里了。我正在客户端使用 Angular 4 编写一个 Web 应用程序,在服务器端使用 Node/Express/Mongo 编写一个 REST API,并使用 Auth0 API 进行身份验证。

在服务器端,我提供静态文件和所有到 Angular 的路由,如下所示:

const distDir = __dirname + "/dist/";
app.use(express.static(distDir));

app.get('/*', function(req, res) {
    res.sendFile(path.join(__dirname + '/dist/index.html'));
});
Run Code Online (Sandbox Code Playgroud)

如果没有为所有 Angular 应用程序提供服务的 app.get 方法,我会收到 404 错误(例如无法 GET /callback)。

问题是 sendFile 发送一个 html 文件,无论调用是什么。在我的应用程序中,第一个调用是针对 js 文件的,如“网络”选项卡 (auth0.min.js) 中所示。所以我得到这个错误:

ERROR SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at e.CPp0.t.json (vendor.b69a8a4fa217eba4fba0.bundle.js:1)
    at e.project (main.0e8acae237e497f792ab.bundle.js:1)
    at e._next (vendor.b69a8a4fa217eba4fba0.bundle.js:1)
    at e.T14+.e.next (vendor.b69a8a4fa217eba4fba0.bundle.js:1)
    at XMLHttpRequest.s (vendor.b69a8a4fa217eba4fba0.bundle.js:1)
    at e.invokeTask (polyfills.043084ca495430fc14f3.bundle.js:1) …
Run Code Online (Sandbox Code Playgroud)

node.js sendfile express auth0 angular

6
推荐指数
0
解决办法
296
查看次数

标签 统计

angular ×1

auth0 ×1

express ×1

node.js ×1

sendfile ×1