我在 Angular 6 上构建了一个应用程序,并且能够直接从 dist 文件夹打开 index.html,但是当我尝试从我的 Node 服务器打开它时,控制台中出现错误:
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 行为 <!文档类型 html >
服务器.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} `);
});
最佳答案
由于您的错误,您可能没有为 Angular 配置正确的公共(public)目录,请尝试此操作,
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname,'dist/ang-node/public'))); // <== correct public directory here
app.get('*',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/ang-node/index.html')); // <== Make sure this is dist/ang-node not dist
});
const port = process.env.PORT || 4000;
app.listen(port,(req,res)=>{
console.log(`server started on port ${port} `);
});
关于node.js - 无法从 Node 服务器打开 Angular 构建应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996890/