Deploy Angular Build With Express JS Project
Deploy Angular Build in Node JS (Express JS) Server
Mean Stack - Education Funda |
Firstly Create Angular Production Ready Build:
~ ng build (run in command line when directory is projectFolder).
for i in dist/*/*; do brotli $i; done
for i in dist/*/*; do brotli $i; done
Deployment Check Angular Build:
You can get a preview of your application using the ng serve --prod command that starts a local HTTP server such that the application with production files is accessible using http://localhost:4200. This is not safe to use for production usage.
For a production usage, you have to deploy all the files from the dist folder in the HTTP server of your choice.
Node JS/Express JS Server File Changes:
//enabling CORS
app.use(cors());
app.use(express.static(path.resolve('./public')));
//api v1 base route set
app.use('/api/v1', router);
//angular front site
app.use(express.static(path.resolve('./public/angular')));
//app.set('view engine', 'pug');
app.get('*', (req, res) => {
res.sendFile(path.resolve('./public/front/index.html'))
});
Above is our app.js file which is default file of node js project, it could be server.js or index.js anything as per your project.
In this file Routing Sequence is matter a lot, firstly put your back-end routes then below that you can put your angular routes otherwise back-end routes will not work.
I hope this blog will be helpful for you, please share your questions/feedback in comments section.
Follow me in LinkedIn.
<> Happy Coding </>
Comments
Post a Comment