Book Image

Web Development with MongoDB and Node.js

By : Jason Krol
Book Image

Web Development with MongoDB and Node.js

By: Jason Krol

Overview of this book

Table of Contents (19 chapters)
Web Development with MongoDB and Node.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
12
Popular Node.js Web Frameworks
Index

Server.js – where it all begins


Whenever you write a Node app, you always need to start somewhere. The typical convention while building servers with Node is that you have a single server.js file located within the root of your project. This file will boot up the server and start the whole process. In our case, this is the file that will create the HTTP server and listen for all HTTP events, which is ultimately the point of our entire application.

We are going to keep our server.js pretty lean so that its contents are very self-explanatory. Any major logic that is going to be executed within this file will actually be defered to external modules hosted within other files.

Before we can do anything within server.js, we need to require a few modules that we're going to work with, specifically Express:

 var express = require('express'),
    // config = require('./server/configure'),
    app = express();

In the preceding code, we are assigning the Express module to the variable express. The config...