【问题标题】:Unable to open angular build application from node server无法从节点服务器打开角度构建应用程序
【发布时间】: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


【解决方案1】:

由于您的错误,您可能没有为 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} `);
});

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2018-07-27
    • 2018-12-05
    • 2021-07-30
    • 1970-01-01
    • 2021-02-22
    • 2018-03-24
    • 2019-05-24
    相关资源
    最近更新 更多