Make Angular run on Node Server with Express JS
makesure you have run “npm run build” or “yarn build” for build angular production mode
yarn build
Install express in Angular proejct use npm or yarn
yarn add express
Create server.js in Angular root project with fill like below
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const port = process.env.NODE_PORT || 4200;
const root = path.join(__dirname, 'dist', 'learning_angular');
app.get('*' ,function(req, res) {
fs.stat(root + req.path, function(err){
if(err){
res.sendFile("index.html", { root });
}else{
res.sendFile(req.path, { root });
}
})
});
app.listen(port);
console.log('Listening on port '+ port);
You can customize path production dist. example is dist/learning_angular
Now you can run “node server.js” in terminal
node server.js