【发布时间】:2019-01-30 11:42:27
【问题描述】:
我在 Angular 6 上构建了一个应用程序,可以直接从 dist 文件夹打开 index.html,但是当我尝试从我的节点服务器打开它时,控制台中出现错误:
Uncaught SyntaxError: Unexpected token <
polyfills.js:1 Uncaught SyntaxError: Unexpected token <
styles.js:1 Uncaught SyntaxError: Unexpected token <
vendor.js:1 Uncaught SyntaxError: Unexpected token <
main.js:1 Uncaught SyntaxError: Unexpected token <
第 1 行为
server.js
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname,'dist')));
app.get('*',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/ang-node/index.html'));
});
const port = process.env.PORT || 4000;
app.listen(port,(req,res)=>{
console.log(`server started on port ${port} `);
});
【问题讨论】:
-
当浏览器请求您的 javascript 文件时,您的应用会发回
index.html。看看stackoverflow.com/questions/26349497/… -
index.html 将出现在 sendFile 请求中,在中间件中我传递父目录的静态路径。该文件正在源中启动,但在控制台中出现了一些错误。
标签: node.js angular mean-stack