loading...
Cover image for Explain me this like I'm five

Explain me this like I'm five

kevinhch profile image Kevin ・1 min read

Hi, yesterday I was talking with a co-workers and we was talking about Node Js, how it works in local to develop API and pretty simples apps, how to connect to a database... but when we started to talk about how it works in Production enviroment, we are lost, i mean, both of us don't know how to deploy, or where is the best way to deploy this types of apps which runs in NODE, he deploy a simple app in Nginx and apache, wich its runs.
But I still have the question, wich server or types or servers are using this plattforms like Digital ocean or Heroku, its worh deploy a Node app in Nginx or Apache? If i have a server, how should i deploy a simple app builded in Node and React (Digital ocean and Heroku are not a option) or what server is the best for this, nginx, apache...

Discussion

pic
Editor guide
Collapse
sroehrl profile image
neoan

Servers are responding to external requests. The ports you want to focus on are 80 (http) and 443 (https). NodeJS servers (like express) usually use ports beyond 1024 due to permissions, most commonly 3000 or 8080.

Now, in production, you will need to serve over http(s) for a browser to reach your app. While you have multiple options to achieve that, using Apache or Nginx is a good option as you probably also want to serve static files and assets.

I will make the following assumptions:

  • you are using a linux system in production
  • your app runs on port 3000

Your Nginx config could then look like this:

server {
    listen 443;
    server_name your_app.com;
    client_max_body_size 50M;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /static {
        root /var/www/your_app;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

NOTE: be aware that you will need to look into an ssl certificate as well to serve over 443. "certbot" is what you want to google for!

Collapse
kevinhch profile image
Kevin Author

Thanks for your answer, I will save this config :D

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

In Heroku, you can deploy Node.js app directly.

But for DO or AWS, Docker is a better option, I think.

But indeed, Nginx seems to have a better performance for serving static files. I don't know much about reverse proxy. I think it is about DevOps and real production environment, which might cost a little money, if you use too much.

But I indeed have touch both Apache and Nginx when I use PHP.

If i have a server, how should i deploy a simple app builded in Node and React (Digital ocean and Heroku are not a option)

Where do you plan to deploy?

Collapse
kevinhch profile image
Kevin Author

Thanks for your answer, no, I just want to know how Heroku or DO serve this type of applications, that's all.

Collapse
thatblairguy profile image
That Blair Guy

That's actually a fairly broad topic, and a lot of it depends on exactly what your application is trying to do. You're probably going to want to do some reading on the #node tag, but here's my best high-level guidance.

Architecture
So, what's your Node app doing? Is it running a public web site? Responding to URLs for a publicly available API? Or is it an application that your front end application just calls for specific calculations? This impacts literally everything else.

Hosting
A commodity PHP host won't cut it for Node. You're going to need one which allows for long-running applications. Generally that means a custom VM, or perhaps something that just allows you to deploy containers. That doesn't have to be Digital Ocean or Heroku, though they do have the kind of set up you're looking for.

Front end web server
There's no requirement for a traditional web server, but it's a common configuration. You can add middleware to Express to serve up static files, but it's generally faster (and already written) to use Apache, Nginx, or something else to serve up files and act as a reverse proxy for the specific routes your application is handling.

Ports
This is a bit of a low-level implementation detail, but seeing as how others have touched on it.... I believe Express defaults to port 5000, but you can set it listen on whatever you want. If you're using a front end server, you'd let the front end server handle port 80 and 443. If Express is handling all the traffic, then the Node app will need to handle those ports instead. (And note, I'm only assuming you're using Express, that's not required for a Node app that responds to HTTP requests; it's just a very popular solution.)

Development vs. Production
I find it's helpful if, to the extent possible, the development environment matches the production environment. You won't install a compiler or an IDE on the production environment, but if the production environment will have a front end web server, it's helpful to have the same front end web server running in the development environment. (I highly recommend you read about "The twelve-factor app for more about this topic, and other helpful guidance.)

Deployment
"Automate all the things." This has nothing to do with Node. If you can't automate everything, automate as much as you can. Can't use Travis, Jenkins, or a similar Continuous Integration tool? Go with a shell script.

I have personal experience with deployments which took three hours per environment and tended to result in production outage because the deployment instructions were a 12 page Word document. Those same deployments dropped to three minutes and stopped breaking production once we wrote a script to handle the config file changes.

Collapse
kevinhch profile image
Kevin Author

Thanks for your answer and all the tips, I have here a very very useful tips, I will read more about node.