Express is a fast, open, minimalist web development framework based on the node. JS platform. So, before using Express, make sure that you have node. js installed.
1. Create a directory as the current working directory:
$ mkdir myapp$ CD MyApp
2. Create a Package.json file for your app with the NPM init command. For more information on how Package.json works, please refer to specifics of npm package.json handling. This command will ask you to enter several parameters, such as the name and version of the app. You can simply press the "enter" key to accept the default settings.
This command generates the Package.json file in the current directory.
$ NPM Init
This utility would walk you through creating a Package.json file.
It is covers the most common items, and tries to guess sane defaults.
See "NPM help JSON" for definitive documentation in these fields
And exactly what they do.
Use ' npm install <pkg>--save ' afterwards-to-install a package and
Save it as a dependency in the Package.json file.
Press ^c at no time to quit.
Name: (MyApp)
Version: (1.0.0)
Description
Entry point: (Index.js)
Test command:
Git repository:
Keywords
Author
License: (ISC)
About to write To/usr/local/lljwork/express/myapp/package.json:
{
"Name": "MyApp",
"Version": "1.0.0",
"description": "",
"Main": "Index.js",
"Scripts": {
"Test": "Echo \" Error:no test specified\ "&& exit 1"
},
"Author": "",
"License": "ISC"
}
Is this OK? (yes)
3. Install Express and save it to the dependency list:
$ NPM Install Express--save
Dules/express
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected], [email protected])
└──[email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
4. Create an app skeleton using the Express builder
$ NPM Install Express-generator-g
[Email protected]/usr/local/lib/node_modules/express-generator
├──[email protected]
├──[email protected] ([email protected])
└──[email protected] ([email protected])
Run the $ express-h command to list the available command-line options, or to verify that Express is installed successfully.
$ express-h
usage:express [Options] [dir]
Options:
-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|sass) (defaults to plain CSS)
--git Add. Gitignore
-F,--force force on Non-empty directory
5. Create an app named MyApp under the current directory
$ Express 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.css
Create:myapp/routes
Create:myapp/routes/index.js
Create:myapp/routes/users.js
Create:myapp/views
Create:myapp/views/index.jade
Create:myapp/views/layout.jade
Create:myapp/views/error.jade
Create:myapp/bin
Create:myapp/bin/www
Install dependencies:
$ cd MyApp && NPM Install
Run the app:
$ debug=myapp:* NPM Start
6. Install all dependent packages
$ cd myapp$ NPM Install
[Email protected] Node_modules/debug
└──[email protected]
[Email protected] Node_modules/serve-favicon
├──[email protected]
├──[email protected]
├──[email protected]
└──[email protected]
[Email protected] Node_modules/cookie-parser
├──[email protected]
└──[email protected]
[Email protected] Node_modules/morgan
├──[email protected]
├──[email protected]
├──[email protected]
└──[email protected] ([email protected])
[Email protected] Node_modules/body-parser
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected]
└──[email protected] ([email protected], [email protected])
[Email protected] node_modules/express
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected])
├──[email protected]
├──[email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected], [email protected])
└──[email protected] ([email protected], [email protected])
[Email protected] Node_modules/jade
├──[email protected]
├──[email protected]
├──[email protected]
├──[email protected] ([email protected])
├──[email protected] ([email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected], [email protected])
├──[email protected] ([email protected], [email protected], [email protected])
└──[email protected] ([email protected], [email protected], [email protected], [email protected])
Applications created with the Express app builder typically have the following directory structure:
. ├──app.js├──bin│ └──www├──package.json├──public│ ├──images│ ├──javascripts│ └── stylesheets│ └──style.css├──routes│ ├──index.js│ └──users.js└──views ├──error.jade ├── Index.jade └──layout.jade79 files
7. Start the service
MacOS or Linux platforms:
$ Debug=myapp NPM Start
Windows Platform:
> Set Debug=myapp & npm start
At this point, a simple server has been built, and started the service, in the browser open the Http://localhost:3000/URL can be seen.
Detailed Reference Express official website: http://www.expressjs.com.cn/
Use Express to build a server