Express ~ Environment Configuration - rohit120582sharma/Documentation GitHub Wiki
An environment variable is basically a variable that is part of the environment in which a process runs and it's value is set outside the application. So typically in hosting environment for node applications, we have environment variable called PORT
.
Use environment variables for anything you need to change or configure in your code that will change based on the environment.
common scenarios:
- Which HTTP port to listen on
- What path and folder your files are located in, that you want to serve
- Pointing to a development, staging, test, or production database
Set an environment variable:
// OS X / Linux:
export PORT=5000
// Windows:
set PORT=5000
const express = require(‘express’);
const app = express();
// Check the environment
if(process.env.NODE_ENV === 'development'){
// Connect to development database
}else if(process.env.NODE_ENV === 'production'){
// Connect to production database
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});