gpt4 book ai didi

node.js - 如何在生产环境中使用 nodejs api 运行 angular 6 应用程序?

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:38 27 4
gpt4 key购买 nike

嗨,我是 angular6 的新手

在开发模式下,我在操作中使用不同端口 (8080) 的 Angular 4200 端口和 Node 。但现在我想将我的应用程序移至生产模式。为此,我使用 ng build 命令构建了我的 Angular 项目。在我得到一个 dist 文件夹后,在根文件夹上创建了。

所以我去了 server.js 文件在这里我添加了我的静态文件

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

app.use('/',(req,res)=>{

res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});

点击此处阅读我的 server.js文件

由此每当我重定向到我的本地主机时,它都会打开但是在 server.js 端我没有得到任何响应。

每当我尝试登录时,我都会收到此错误。 enter image description here

谁能帮我解决这个问题

最佳答案

我认为您的代码中有两处错误。

首先,更新你的代码

来自:-

app.use('/',(req,res)=>{

res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});

至:- 此代码需要添加到您的app.listen(app.get('port'))代码

之前
app.get('*', function(req, res) {
res.sendfile('./public/MDProject/index.html');
// load the single view file (angular will handle the page changes on the front-end)
});

其次,从您的 public 文件夹提供您的文件(在您的情况下,我认为它是 dist 文件夹)。所以像这样更新你的代码。

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

关于node.js - 如何在生产环境中使用 nodejs api 运行 angular 6 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53099973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com