简体   繁体   English

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

[英]Unable to open angular build application from node server

I built an app on angular 6 and directly able to open index.html from dist folder but when i tried it opening from my node server, error is coming in console as: 我在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 <

with line 1 as < ! 第1行为<! doctype html > doctype html>

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

As your error you may not configured the right public directory for the angular, Try this, 由于发生错误,您可能没有为Angular配置正确的公共目录,请尝试以下操作,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM