Nodejs Server Setup - pcimino/nodejs-restify-mongodb GitHub Wiki

App.js and Configuration

Variable Setup

The top of the app.js file has two sections (which could easily be one section). The first sets up the modules used in app.js. The second brings in configuration settings.

Path variables are used to locate other resources.

Restify is a module for building strict REST APIs. Has quite a few features, which I may actually use someday.

Mongoose adds utilities for accessing MongoDB from Nodejs. Mongoose simplifies object handling by supporting schema templates.

Passport is used to simplify authentication and authorization.

fs is a utility module for reading files.

Node Mailer, Address Parser and Mail Preview are used for wiring in and testing email.

Configuration

The config variable contains the object from config.js defined by the NODE_ENV environment variable (see one of the node start scripts). If this isn't defined, development is the default.

Database setup

Fairly straightforward

  • Build a connection string based on data set in the configuration
  • Log the connection string to the console (totally unnecessary, but nice to see something happen on startup)
  • Assign db to the connection (shortcut)
  • Connect to the database and open the connection

Models

Iterate through all files in the /models directory and pull them in.

Configure the server

For a full list of configuration settings available, refer to the docs here.

Load configurations

Passport and Restify setups are called, and then the routes (API handlers) are loaded.

Start Listening

The server is now ready to start listening. The port number can be overridden using the environment variable PORT, or uses the port form the configuration. Port number is output to the console.

Next, setting up with the Configuration File

Return Home