Book Image

Node.js Blueprints

By : Krasimir Stefanov Tsonev
Book Image

Node.js Blueprints

By: Krasimir Stefanov Tsonev

Overview of this book

Table of Contents (19 chapters)
Node.js Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Running the server and delivering the assets


In Chapter 5, Creating a To-Do Application with Backbone.js, we built an application with Backbone.js, and we used two helper functions: serveAssets and respond. The purpose of these functions was to read our HTML, CSS, and JavaScript files and send them as a response to the browser. We will use them again here.

Let's first start by defining the global variables, as follows:

var http = require('http'),
  fs = require('fs'),
  port = 3000,
  files = [],
  debug = true;

The http module provides methods to create and run the Node.js server, and the fs module is responsible for reading the files from the filesystem. We are going to listen on port 3000 and the files variable will cache the content of the read files. When debug is set to true, the assets will be read on every request. If it is false, their content will be fetched only the first time, but every future response will contain the same code. We are doing this because while we are developing...