无法从节点服务器打开角度构建应用程序

时间:2018-08-24 03:07:57

标签: node.js angular mean-stack

我在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行为<! doctype html>

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} `);
});

1 个答案:

答案 0 :(得分:0)

由于您的错误,您可能未为角度配置正确的公共目录,请尝试此操作

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} `);
});