【问题标题】:Error in running Angular 6 from nodeJS server从 nodeJS 服务器运行 Angular 6 时出错
【发布时间】:2018-07-20 17:29:15
【问题描述】:

我的项目结构是

myApp-
     |-dist -
             |- myApp -
                      |- index.html
                      |- main.js

     |-server- 
             |- app.js

     |-src-
             | - app
             | - assets
             | - index.html
             | - main.ts

我的app.js 文件

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();

var port = process.env.PORT || 3000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));


app.use(express.static(path.join(__dirname, 'dist')));


app.get('/',(req,res)=> {
  res.sendFile(path.join(__dirname, '../dist/myApp/index.html'));
});

var server = http.createServer(app);

server.listen(port, ()=>{
  console.log('Server running at port ', port)
});

Angular 文件和默认的一样

但它只显示一个空白页面。 我知道它正在到达index.html,因为它更改了 index.html 中的标题名称,但它没有到达 app.component.html

index.html 页面是

   <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>BtechApp</title>
      <base href="/BtechApp">

      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    </head>
    <body>
      <app-root></app-root>
    </body>
    </html>

当我检查该页面时,它显示错误

Uncaught SyntaxError: Unexpected token <

另外,我在网上查了一下,app.js 中 index.html 的路径是

dist -
     | - index.html

但在我的情况下,路径是

dist -
     | - myApp -
               | - index.html

注意 - 我发现问题出在我的 app.js 文件中,您可以在下面查看我的答案。

【问题讨论】:

  • 角码在哪里?控制台有错误吗?
  • @Clint 在检查页面上显示Uncaught SyntaxError: Unexpected token &lt;
  • 听起来你的 javascript 有语法错误。对于问题中的问题,我们无能为力。
  • 一般来说,无论如何,在angular-cli中,dist文件夹是放置src文件夹编译的代码的地方。您的 index.html 文件中明显缺少 script 元素,这让我相信编译过程中出了点问题。
  • @HereticMonkey 我使用ng build命令编译。

标签: javascript node.js angular typescript


【解决方案1】:

index.html 有标签而不是文档的类型。因此,当节点尝试解析您的 index.html 时,它会将第一个字符作为

【讨论】:

  • 我应该把那个标签放在哪里?
  • 不要忘记关闭文档末尾的 html 标签
  • 我正在关闭 index.html 中的每个标签。你可以在上面看到。
【解决方案2】:

我只是给出了 index.html 的错误路径

所以新的 app.js 将是

app.use(express.static(__dirname + '/dist/newApp'));

app.get('/', (req,res)=>{
  res.sendFile(path.join(__dirname));
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多