I have a MEAN Stack app that I need to host on CPanel and I have never deployed an app before. It is easy to deploy Angular part of the app by creating a bundle in dist folder using the command:
ng build --prod
But I would like to know how should I post my backend code on the server. Is there any command/commands that can create a minified bundle like in Angular?
I would also like to know if I need to install some npm packages to make sure my app runs smoothly on the server? Like I keep reading about PM2 and Nginx when I googled about deploying nodejs. So what are their benefits?
Please mention any articles or videos that might be useful.
Angular version 7
Node version 8.11.3
Ubuntu 16.04
You can deploy the code as it is and it will work!
You don't need to minify the code. The VM is fast enough to run it. Minification is usually done to reduce the size of the file when sending it from the Server to the Client, and since you're not sending any file, you don't need to worry about it.
I recommend PM2 for managing the Node processes once you reach production status. It includes logs, auto-restart and many cool features that will help you out.
If you're concerned about the size of your node_modules directory, perhaps you can identify which packages are being used during development only (for example, testing tools like Jest and Mocha, transpilers like Babel, Typescript, etc). Place these inside the "devDependency" key in your package.json file. Then, on production, run npm install --production and it will not install whatever you listed as a devDependency. That should produce smaller node_modules.
If you're following The Twelve Factors, then also create an environment file for the production-only credentials.
For completeness, if you're using Babel/Typescript or any other tool like that then you do need to transpile the project to a Javascript Code that Node can understand. Run the respective commands to generate a production build.
About Nginx, some people like to place Nginx as a reverse proxy to let it serve the static assets, while for API requests they forward it to the Node API. This is "best practices" since Nginx can be easily tweaked to serve the static assets faster and includes several options for cache, security protection, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With