-
Notifications
You must be signed in to change notification settings - Fork 60
NodeJS Setup for Production (work in progress)
The NODE_ENV
environment variable specifies the environment in which an application is running (usually, development
or production
). One of the simplest things you can do to improve performance is to set NODE_ENV
to “production.”
Setting NODE_ENV to “production” makes Express:
- Cache view templates.
- Cache CSS files generated from CSS extensions.
- Generate less verbose error messages.
While the first two points make no difference for us, as we serve just a back-end REST API, we still don't want verbose error messages and production, there are some environment specific configurations like Keycloak setup...
Because we have environment-specific code, we can check the value of NODE_ENV
with process.env.NODE_ENV
.
Be aware that checking the value of any environment variable incurs a performance penalty, and so should be done sparingly.
For the production server we want the NODE_ENV
permanently set to production
. In Ubuntu, the underlying server for #codingmarks, we can set a system-wide environment variable in the /etc/environment file:
$ sudo vim /etc/environment
Append the following at the end of the file:
NODE_ENV=production
This is set for the whole system, rather for just a particular user.
Now logout and login again and now we can see the system wide environment variable:
$ printenv | grep NODE_ENV
NODE_ENV=production
- https://expressjs.com/en/advanced/best-practice-performance.html#in-environment
- https://help.ubuntu.com/community/EnvironmentVariables
- https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting
- https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04