如何在生产上使用nodejs api运行angular 6应用程序? [英] how to run angular 6 app with nodejs api on production?

查看:117
本文介绍了如何在生产上使用nodejs api运行angular 6应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angular6的新手

Hi I am new to angular6

在开发模式下,我在操作中使用了角度4200端口和其他端口(8080)中的节点. 但现在我想将我的应用程序移至生产模式. 为此,我已经使用 ng build 命令构建了我的角度项目.在我得到dist文件夹之后,在根文件夹上创建了

In development mode I have worked in angular 4200 port and node in different port (8080) in my operations. but now I want to move my app into production mode. for that I had build my angular project by using ng build command. after i get a dist folder is created on the root folder.

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

so i went to server.js file here I had add my static file

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方面,我没有任何响应.

by this when ever I redirect to my local host it is opening but in server.js side i didn't get any response.

每次尝试登录时,都会出现此错误.

when ever I try to login I am getting this error.

谁能帮我解决这个问题

推荐答案

我认为您的代码中有2处错误.

I think there are 2 things wrong here in your code.

首先,更新您的代码

发件人:-

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

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

收件人:-此代码需要在您的app.listen(app.get('port'))代码之前添加

To:- This code needs to be added just before your app.listen(app.get('port')) code

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文件夹).因此,像这样更新您的代码.

Second, Serve your file from your public folder(In your case I think it is the dist folder). So update your code like this.

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

这篇关于如何在生产上使用nodejs api运行angular 6应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆