Expressjs Getting Started Guide (2)
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
Next to the previous article "expressjs Getting Started Guide"
6. Use Express (1) to generate applications
The express framework is bound with an executable script named Express (1 ). If you use NPM to globally install the express framework, express can be used everywhere.
> NPM install-G Express
The Express (1) tool provides a simple way to generate the application skeleton, and it has a certain scope of use. For example, it only supports a limited number of template engines, while express itself supports the template engine built by any node. You can use the following command to View Details:
> Express -- Help
Ps d: \ TMP \ node \ hello-world> Express -- Help
Usage: Express [Options]
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) (ults to plain CSS)
-F, -- Force on non-empty directory
If you want to generate applications that support ejs, stylus, and session sessions, do this:
> Express -- sessions -- CSS stylus -- ejs MyApp
D: \ TMP \ node \ hello-world> Express -- sessions -- CSS stylus -- ejs MyApp
Create: MyApp
Create: MyApp/package. JSON
Create: MyApp/APP. js
Create: MyApp/Public
Create: MyApp/public/javascripts
Create: MyApp/public/Images
Create: MyApp/public/stylesheets
Create: MyApp/public/stylesheets/style. styl
Create: MyApp/routes
Create: MyApp/routes/index. js
Create: MyApp/routes/user. js
Create: MyApp/Views
Create: MyApp/views/index. ejs
Install dependencies:
$ CD MyApp & NPM install
Run the app:
$ Node app
Like other node applications, dependency packages must be installed.
> Cd MyApp
> NPM install
Then run MyApp and execute:
> Node MyApp. js
Use a browser to access: http: // localhost: 3000/
You can see:
Express
Welcome to express
This is the whole process of creating a simple application. Remember that the express framework is not bound to any specified directory structure,
VII. Express framework Description
Express framework is a good web development framework in the node. js third-party framework. In addition to providing high-level interfaces for the HTTP module, express also provides many functions, including:
1) route control;
2) template parsing support;
3) dynamic view;
4) user sessions;
5) csrf protection;
6) static file service;
7) Error controller;
8) access logs;
9) cache;
10) plugin support.
It should be noted that express is not an all-encompassing all-around framework (like rails or Django implementing the template engine or even ORM). It is just a lightweight Web framework, its main function is to encapsulate common operations in HTTP. More functions require plug-ins or integration with other modules.
For example:
var express = require('express');var app = express();app.use(express.bodyParser());app.all('/', function(req, res){res.send(req.body.title + req.body.text);});app.listen(3000);