[英]how to run angular 6 app with nodejs api on production?
Hi I am new to angular6 嗨,我是angular6的新手
In development mode I have worked in angular 4200 port and node in different port (8080) in my operations. 在开发模式下,我在操作中使用了角度4200端口和其他端口(8080)中的节点。 but now I want to move my app into production mode.
但现在我想将我的应用程序移至生产模式。 for that I had build my angular project by using
ng build
command. 为此,我已经使用
ng build
命令构建了我的角度项目。 after i get a dist folder is created on the root folder. 我得到一个dist文件夹后,在根文件夹上创建一个。
so i went to server.js file here I had add my static file 所以我去了server.js文件,我已经添加了我的静态文件
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
click here to read my server.js file 点击这里阅读我的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. 这样,当我重定向到本地主机时,它就打开了,但是在server.js端,我没有任何响应。
when ever I try to login I am getting this error. 每当我尝试登录时,都会出现此错误。
can anyone help me to solve this 谁能帮我解决这个问题
I think there are 2 things wrong here in your code. 我认为您的代码中有2处错误。
First, update your code 首先,更新您的代码
From:- 从:-
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
To:- This code needs to be added just before your app.listen(app.get('port'))
code 到:- 此代码需要在您的
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)
});
Second, Serve your file from your public
folder(In your case I think it is the dist
folder). 其次,从您服务您的文件
public
文件夹(在你的情况,我认为它是dist
文件夹)。 So update your code like this. 因此,像这样更新您的代码。
app.use(express.static(__dirname + '/dist/MDProject'));
after I removed this line in my server.js file 在我的server.js文件中删除此行之后
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
and I paste this line before port creation. 然后在创建端口之前粘贴此行。
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
now it is working fine. 现在工作正常。
Use this to go to index page. 用它去索引页。
app.get('*', (req, res) => {
res.sendfile('./public/MDProject/index.html');
})
with that you have to make folder in public in node 这样,您必须在节点中将文件夹公开
app.use('/', express.static('/dist/MDProject'))
Then in your app dist folder -> index.html 然后在您的应用程序dist文件夹中-> index.html
<base href="http://192.168.1.1:3000/admin-panel/">
This is my working code. 这是我的工作代码。 Let me know if you have still any issue.
让我知道您是否还有任何问题。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.