nodejs express app deploying to production
Asked Answered
S

2

13

Sorry if this is a basic question, I am still wrapping my head around nodejs deployments. I have an app written on nodejs which I want to deploy to production. So far for testing purposes I have used Express. However from what I know Express is a dev server and not a production server. How do I deploy the nodejs app to production and what is the server I should use. Plus I have a lot of code written for express like routes and middleware, how does this work when I deploy to another server?

Shope answered 29/7, 2016 at 6:18 Comment(6)
Provide more details like type of the server you are going to deploy? Is it Cloud based? Or Check out this for various deployments For Azure Cloud : azure.microsoft.com/en-in/documentation/articles/… For IIS Windows : hanselman.com/blog/… For Linux : digitalocean.com/community/tutorials/…Arbil
its cloud based on Azure (web role). My main concern is, does this mean I have to abandon the Express code I wrote and try out something else?Shope
Not required.. If its azure its really very easy deploy. Before publishing open up the node.js command line - > go to your project folder and then enter flatten-package. if you dont have his npm install it and then run it. Since the npm modules will have nested modules and it the only issue that will come up when doing a webdeploy. Access your azure webapp and download the publishing profile. and in the project - RightClick -> publish select Azure and import the publish setting. And you are now ready to publish and it will work. try it and post if you face any issues.Arbil
Where did you get the notion that Express cannot be used for production? As you noted, if you can't use Express for production you'd have to basically rewrite your entire app, so what would be the point of using it at all?Rollo
I thought Express was a simple development server and not to be used for production, please correct me if I am wrong. Plus what are the standard web servers used to deploy nodejs applications to production?Shope
Possible duplicate of How to package & deploy Node.js + express web application?Illbred
I
5

When you create your application with express.js, all express modules have been specified in package.json and will be installed as npm modules.

All you need to do is just install node.js on your production servers, put your code there, run npm install, then start the web server with NODE_ENV=production param. It would be a plus if you can use grunt or gulp to process static assets (js / css minification, ...) to optimize the performance for production mode.

For more information, you can take a look here: https://expressjs.com/en/advanced/best-practice-performance.html

Irrecusable answered 29/7, 2016 at 7:24 Comment(0)
M
3

You can use pm2 combined with ExpressJs to deploy on remote server. Basically pm2 makes your code run forever in background task with cluster mode.

Basically you need to put your files into remote server and create a .json or yaml file for pm2 to understand what will be the process.

Then you should install libs and modules through npm install. After this you need to install pm2 on your remote server using npm i -g pm2.

After all of them, you can use pm2's basic cli commands to deploy. pm2 start process.yml --env=production is a basic example for deployment. You can use pm2 stop process.yml for stopping the task. You can also use pm2 monit for system monitoring. If you want to see the logs, you can run pm2 logs. If you want to restart or update: pm2 reload all or pm2 update helps.

For remote server, you can use clouds for best performance. You can look the pricing of AWS, Azure, GCloud, Heroku or DigitalOcean. They are providing free tiers and you can search for them.

Mirabel answered 19/11, 2020 at 23:19 Comment(4)
Thanks for your input, @cangokceaslan! I've developed an Express backend and a React frontend. And now I'm jumping like a ball around for inputs to connect the dots on how to integrate the React app into my backend Express app, and subsequently "compile" the backend for best performance. So far, as I understood, the Express app wouldn't be compiled manually, say like a J2EE application to a WAR file; but rather it's simply run using an orchestration tool such as pm2.Strikebreaker
Well depends but most of the time Node.js apps are not compiled because it runs with node. You can compile or transpile your code with BabelJs however there is no reason to do that. Secondly you always need a process manager to run a task forever. You can choose pm2, docker or forever for deployment. Unlike Java, Node.js runs on terminal with basic commans like Python.Mirabel
Thanks @Mirabel for your input, it helped. :)Strikebreaker
Nice to see this :)Mirabel

© 2022 - 2024 — McMap. All rights reserved.