嗨,我是 Angular6 的新手
在开发模式下,我在操作中使用了 Angular 4200 端口和不同端口(8080)的 Node 。
但现在我想将我的应用程序转移到生产模式。
为此,我使用 ng build
命令构建了我的 Angular 项目。在我得到一个 dist 文件夹后,在根文件夹上创建了一个 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 端我没有得到任何响应。
谁能帮我解决这个问题
最佳答案
我认为您的代码中有两处错误。
首先,更新您的代码
来自:-
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/