Skip to content

Instantly share code, notes, and snippets.

@bradseverance
Created January 10, 2014 19:19
Show Gist options
  • Select an option

  • Save bradseverance/8360734 to your computer and use it in GitHub Desktop.

Select an option

Save bradseverance/8360734 to your computer and use it in GitHub Desktop.
Node-config
node-config
1) install the config module
npm install config
2) create a directory called ‘config’ in your application directory
3) create a file inside the ‘config’ directory called ‘default.json’
4) create a file inside the ‘config’ directory called ‘localhost.json’
5) create a file inside the ‘config’ directory called ‘development.json’
6) create a file inside the ‘config’ directory called ‘stage.json’
7) create a file inside the ‘config’ directory called ‘production.json’
8) create a file inside the ‘config’ directory called test.json’
In each json file, setup a corresponding mongodb connection string:
For example, in localhost:
{
"dbString" : "mongodb://localhost:27017/homepage-app"
}
In your package.json file add the following scripts:
"scripts": {
"start": "node server.js",
"localhost": "NODE_ENV=localhost node-dev server.js",
"development": "NODE_ENV=development node-dev server.js",
"test": "NODE_ENV=test node server.js",
"stage": "NODE_ENV=stage node server.js",
"production": "NODE_ENV=production node server.js"
}
***********************************************************************************
In your db file, when connecting to the database, do something like this:
// fire up the database
var mongoose = require('mongoose');
// mongodb connection string
var dbString = require('config').dbString;
// log
console.log("Connecting to " + dbString + " ...");
// connect to mongo db
mongoose.connect(dbString);
// add to log (this should be callback)
console.log("Connected!")
// export the module to the application
module.exports = mongoose.connection;
***********************************************************************************
To start to the application run the corresponding npm script. For example, to run locally, do:
npm run-script localhost
To run in development
npm run-script development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment