O'Reilly logo

Node for Front-End Developers by Garann Means

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

File Organization

With Node installed, we can begin creating the scaffolding for a web application. Since we control how our application serves files, we can put them almost anywhere we want. There are, however, a few conventions it makes sense for us to follow. It’s expected that the main application file we want to run is in the root directory of the site, as is the package.json file (which we’ll create below). Some common directories we might expect to find in the root would include:

node_modules

Your locally installed modules from npm

lib

Utilities and other custom modules that belong to your application

public, www, or similar

The static, client-side piece of your application

When setting up a directory structure, all that really matters is whether it makes sense to you. If you’ll be using the Model-View-Controller (MVC) pattern for your application, you may choose to have models, views, and controllers directories in your root. If you’re going to use the Express application framework or model your application’s organization on Express, you may have a root directory called routes. Aside from keeping your organization clear and consistent in case someone else needs to work with it, being in control of how your application finds and delivers files means you can put them wherever you think they belong.

We also want to create a package.json file, which is a manifest for our application. This file is especially important for modules that will be published or shared, but it should also be present for our local application. There are lots of things we might add to a package.json file, but for now let’s create a simple one with some meta information about the application and a couple of dependencies:

{
  "name": "myNodeApp",
  "author": "Jaime Developer",
  "description": "my test node.js application",
  "version": "0.0.1",
  "dependencies": {
    "connect": "1.8.x",
    "express": "2.5.x"
  },
  "engine": "0.6.x",
  "main": "app.js"
}

Most of those keys are exactly what they sound like. The last two, engine and main, refer to the version of Node and the path to the main application file, respectively. The dependencies object is important to note, as it will come in handy if we ever want to move this application. If that object contains all the npm modules our application uses and their correct versions, we can run the command npm install from the root of our application’s new home to install all the required modules at once.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required