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.
Get Node for Front-End Developers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.