Express Web Application Development
上QQ阅读APP看书,第一时间看更新

Auto-generating an Express app

The process of creating the manifest file, the app.js file, the views, and other directories and files can become a tedious chore as we start to work on multiple projects. To automate this process, we can use the express command-line tool.

To refresh you memory, we first encountered the express command-line tool while learning how to install Express. We were told that it generates Express app skeletons; now we have a fairly good idea what it might do.

Using its help option (-h), let's ask express how it works and what its options are:

$ express –h

 Usage: express [options] [directory]

 Options:

 -h, --help output usage information
 -V, --version output the version number
 -s, --sessions add session support
 -e, --ejs add ejs engine support (defaults to jade)
 -J, --jshtml add jshtml engine support (defaults to jade)
 -H, --hogan add hogan.js engine support
 -c, --css <engine> add stylesheet <engine> support (less|stylus) (defaults to plain css)

 Directory:

An optional directory where to create the app (defaults to pwd)
Note

I have added the description for the [directory] parameter myself, it is missing in the official express help screen. If you don't specify a directory, the current directory is assumed to be the target directory.

So, the express command accepts some options and an optional directory to auto-generate an app. Let's create a new app using our newfound knowledge of express:

$ express --sessions ~/auto-express
create : /Users/yaapa/auto-express
create : /Users/yaapa/auto-express/package.json
create : /Users/yaapa/auto-express/app.js
create : /Users/yaapa/auto-express/public
create : /Users/yaapa/auto-express/public/javascripts
create : /Users/yaapa/auto-express/public/images
create : /Users/yaapa/auto-express/public/stylesheets
create : /Users/yaapa/auto-express/public/stylesheets/style.css
create : /Users/yaapa/auto-express/views
create : /Users/yaapa/auto-express/views/layout.jade
create : /Users/yaapa/auto-express/views/index.jade
create : /Users/yaapa/auto-express/routes
create : /Users/yaapa/auto-express/routes/index.js
create : /Users/yaapa/auto-express/routes/user.js
install dependencies:
 $ cd /Users/yaapa/auto-express && npm install

run the app:
 $ node app

You can see that the command created a bunch of file and directories, and exited with the instructions for installing the dependencies and starting the app.

On running the npm install command in the app directory, you will see the familiar dependency installation process seen earlier at the beginning of the chapter. Once the dependency installation process is completed, take a look at the content of the directory:

That's exactly what we already created! Well, almost, except for a new directory named routes, and a file named layout.jade in the views directory. Don't be too bothered by them at this stage. The routes directory will become clear to you in Chapter 3, Understanding Express Routes and the layout.jade file in Chapter 5, The Jade Templating Language.

So now you know, you don't have to sweat creating all the files and directories for each of your apps, the express command is there to take care of them for you.

Note

The auto-generated files, directories, and the directory structure created by the express command is called a skeleton app. It is called so, because it is a barebones app, upon which you can start building your app.

Manually creating the files and directories is the way to go when starting to learn Express, but once you know what you are doing and start working on real projects, you should use the express command to save yourself a lot of time.

One thing to note about express-generated files, directories, and the directory structure is that, they are just suggestions—you are free to modify them to fit your needs anytime and however you want them.

Note

Even though we created a new app directory called auto-express, to maintain continuity, all upcoming examples will be continued in our original app directory express-app.