代码之家  ›  专栏  ›  技术社区  ›  Rupesh

从nodeJS服务器运行角度6时出错

  •  0
  • Rupesh  · 技术社区  · 7 年前

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

    角度文件与默认值相同

    但它只显示了一个空页。 我知道它正在接近 index.html 因为它更改了index.html中标题的名称,但是没有到达 app.component.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文件中,你可以在下面查看我的答案。

    2 回复  |  直到 7 年前
        1
  •  0
  •   DP3    7 年前

    index.html有标记,而不是文档的类型。所以当node试图解析index.html时,它得到的第一个字符是<这就是它所抱怨的。放置html标记应该会有帮助。

        2
  •  0
  •   Rupesh    7 年前

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

    所以新的app.js将

    app.use(express.static(__dirname + '/dist/newApp'));
    
    app.get('/', (req,res)=>{
      res.sendFile(path.join(__dirname));
    });
    
    推荐文章