Express by Examples
Installation
Express is the most popular framework for NodeJS.
This chapter will explain how to install Express and how to create your working environment.
#npm install -g express
This insatlls express framework globally because we want other Linux users can use express too. Express is installed as a node module inside /usr/lib/node_modules/express/ directory.
#npm install -g express-generator
This installs utility app for automatic generation of express environment.
Thus you can install all necessery files into your project with command:
$express myproject
create : myproject
create : myproject/package.json
create : myproject/app.js
create : myproject/public
create : myproject/public/javascripts
create : myproject/public/stylesheets
create : myproject/public/stylesheets/style.css
create : myproject/routes
create : myproject/routes/index.js
create : myproject/routes/users.js
create : myproject/views
create : myproject/views/index.jade
create : myproject/views/layout.jade
create : myproject/views/error.jade
create : myproject/public/images
create : myproject/bin
create : myproject/bin/www
After that install dependencies:
$cd myproject
$npm install
package.json contains
{
"name": "myproject",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.10.2",
"cookie-parser": "~1.3.3",
"debug": "~2.1.1",
"express": "~4.11.1",
"jade": "~1.9.1",
"morgan": "~1.5.1",
"serve-favicon": "~2.2.0"
}
}
To see other options type:
$express -h
-h, --help output usage information
-V, --version output the version number
-e, --ejs add ejs engine support (defaults to jade)
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
Notice that we can use express command in a linux shell because of existance /usr/lib/express file.