Configure Nodejs for Production
Setup a webserver
Nodejs runs best in production with a reverse-proxy setup. Let’s configure nginx to serve static assets directly, handle compression, and proxy connections into nodejs through node’s builtin server.
Add the following to your
boxfile.yml to make nginx available to the runtime:
run.config: # add nginx package extra_packages: - nginx
Now add the following nginx config file into your project, at
Add webs and workers
For your app to run in production, at the very least you’ll need a web component. There is also a good chance you’ll want some sort of job queue to send emails, process jobs, etc. These would all be ideal tasks for a worker component.
Specify web components
You can have as many web components as your app needs by simply adding them to your existing
# add a web component and give it a "start" command web.main: start: nginx: nginx -c /app/config/nginx.conf node: node YOUR_APP.js
In the above snippet
main is the name of web component and can be anything you choose (it is only used as a unique identifier).
Specify worker components
You can have as many worker components as your app needs by simply adding them to your existing
# add a worker component and give it a "start" command worker.main: start: node YOUR_WORKER.js
In the above snippet
main is the name of the worker component and can be anything you choose (it is only used as a unique identifier).
You can visit the log_watch doc for more information about this node.
We can have nodejs compile assets during the deploy process by adding an extra step:
deploy.config: extra_steps: - COMMAND TO COMPILE ASSETS
To migrate data as part of the deploy process you can add a
before_live hook, which will run just before the new instances are started.
Add a deploy hook
Run a task each time we deploy. In your existing boxfile.yml add the following code:
deploy.config: before_live: web.main: - COMMAND TO MIGRATE DB
With your app configured for running in production, whats next? Think about what else your app might need and hopefully the topics below will help you get started with the next steps of your development!