首页 文章

Bundeled angular 2 app with express

提问于
浏览
0

好吧所以我使用webpack捆绑/构建了一个常规的角度cli项目prod,然后获取dist文件夹的内容并将它们放入另一个快速安装的项目中 .
码:

var express = require('express');
var app = express();
var path = require('path');

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/' + 'index.html')
});

app.listen(3000, function() {
    console.log('Example app listening on port 3000!');
});

问题是,当我运行这个时,我会在on:// localhost:3000 /上获得一个“loading ...”页面 . 当我检查控制台时,我收到了这些错误

[无法加载资源:服务器响应状态为404(未找到):// localhost:3000 / inline.js
无法加载资源:服务器响应状态为404(未找到):// localhost:3000 / styles.b52d2076048963e7cbfd.bundle.js
无法加载资源:服务器响应状态为404(未找到):// localhost:3000 / main.d27eab344e583f765067.bundle.js
无法加载资源:服务器响应状态为404(未找到):// localhost:3000 / styles.b52d2076048963e7cbfd.bundle.js
无法加载资源:服务器响应状态为404(未找到):// localhost:3000 / main.d27eab344e583f765067.bundle.js]

任何人都可以解决这个问题或找出问题所在?任何帮助是极大的赞赏 . 我'm doing this is that once the app goes to production I don'有权访问"npm",所以我没有任何ang-cli命令,只有节点命令 . 实现套接字IO也是我们走捷径的另一个原因 . 如果有其他方式可以请告诉我 .

谢谢

2 回答

  • 0

    您需要提供其余的静态文件,因为您现在只发送index.html .

    将您的角度应用程序放在某个目录中,例如public,并执行以下操作:

    app.use(express.static('public'));
    app.get('/', function(req, res) {
      res.sendFile(__dirname + '/public' + 'index.html')
    });
    
  • 0

    我发现这个问题的解决方案是将2个组件放在单独的目录中,并以这种方式构建它们(不要忘记每次更改到comp . 目录):

    ng build --base-href /first/dist
    ng build --base-href /second/dist
    

    那么你将两个组件放在两个不同的目录中

    在快递:

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

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

    他们应该是一个更好的解决方案,但我还没有找到它

    希望能帮助到你

    Gregoire - 您可以在www.agenda.tools找到工作现场

相关问题