How and where to deploy an Angular 8 web application that needs two servers concurrently [MEAN stack]

限于喜欢 提交于 2019-12-11 18:00:30

问题


I have a web application. It's a blog and question-answer website made with MEAN stack making CRUD operations using Mongodb. Before this I have deployed many applications built in react and angular wherein only one server was required i.e. npm start as there was no back-end server code. But this time I decided to keep my back-end code separate from my front-end code. server.js is in a different folder and is running on port:3000, while application is running on localhost:4200. I am using cors for cross origin communication. The project is open source. I have uploaded on github. Here's the repo. Consider it all yours. Please at least give me some hint how to deploy this project. I'm ready to buy domain and VPS also.

PS: Please go through README.md


回答1:


for production build of the angular application you have to use ng build --prod with these command you will get the compiled version of your application inside the dist folder serve this dist folder using nodejs

var express = require("express");
var compression = require('compression')

var port = 7600;

var app = express();
app.use(compression());

app.use("/", express.static(__dirname + '/dist', { etag: true, maxAge: '3d' }));

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

app.listen(port, function() {
    console.log("Express server listening on port " + port);
})

you can use nodejs to create server or expressjs just your port number should be different than your backend server




回答2:


  1. You have to use ng build --prod for compile your development file.
  2. Then you need to setup your node.js file. please check the code
  3. Then just upload all the server files and dist folder to server.

  4. And run the server using pm2 . You have to install it from please check this link

  5. And run just pm2 start server.js

That's it. Now your fron-end and back-end run together on your specific port.



来源:https://stackoverflow.com/questions/57969049/how-and-where-to-deploy-an-angular-8-web-application-that-needs-two-servers-conc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!