Tanzeel
Tanzeel

Reputation: 4998

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

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

Upvotes: 0

Views: 982

Answers (2)

Sourav Golui
Sourav Golui

Reputation: 633

  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.

Upvotes: 1

Raj Rana
Raj Rana

Reputation: 429

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

Upvotes: 1

Related Questions