使用Express搭建服務器

Express是基於Node.js平臺,快速、開放、極簡的web開發框架。因此,使用Express以前,請確保已安裝Node.jsjavascript

1.建立一個目錄做爲當前工做目錄:css

$ mkdir myapp
$ cd myapp

 

2.經過npm  init命令爲你的應用建立一個package.json文件。欲瞭解package.json是如何起做用的,請參考 Specifics of npm’s package.json handling。此命令將要求你輸入幾個參數,例如應用的名稱和版本。可直接按「回車」鍵接受默認設置。html

此命令會在當前目錄下生成package.json文件。java

$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.node

See `npm help json` for definitive documentation on these fields
and exactly what they do.git

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.web

Press ^C at any 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:express

{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}npm


Is this ok? (yes)json

敲了這麼多回車,讓咱們看一下這個package.json裏究竟有什麼。

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

沒錯,就是這配置~

 

3.安裝Express並將其保存到依賴列表中:

$ npm install express --save

dules/express
├── escape-html@1.0.2
├── merge-descriptors@1.0.0
├── array-flatten@1.1.1
├── cookie@0.1.3
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.3.0
├── range-parser@1.0.2
├── vary@1.0.1
├── path-to-regexp@0.1.7
├── etag@1.7.0
├── content-type@1.0.1
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── serve-static@1.10.0
├── depd@1.0.1
├── qs@4.0.0
├── finalhandler@0.4.0 (unpipe@1.0.0)
├── debug@2.2.0 (ms@0.7.1)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1)
├── accepts@1.2.13 (negotiator@0.5.3, mime-types@2.1.7)
├── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)
└── send@0.13.0 (destroy@1.0.3, ms@0.7.1, statuses@1.2.1, mime@1.3.4, http-errors@1.3.1)

 

4.使用express生成器建立一個應用的骨架

$ npm install express-generator -g

express-generator@4.13.1 /usr/local/lib/node_modules/express-generator
├── sorted-object@1.0.0
├── commander@2.7.1 (graceful-readlink@1.0.1)
└── mkdirp@0.5.1 (minimist@0.0.8)

運行$ express -h命令,可列出所用可用的命令行選項,也能夠檢驗express是否安裝成功。

$ 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.在當前目錄下建立一個名爲myapp的應用

$ 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.安裝全部依賴包

$ cd myapp $ npm install

debug@2.2.0 node_modules/debug
└── ms@0.7.1

serve-favicon@2.3.0 node_modules/serve-favicon
├── fresh@0.3.0
├── ms@0.7.1
├── etag@1.7.0
└── parseurl@1.3.0

cookie-parser@1.3.5 node_modules/cookie-parser
├── cookie@0.1.3
└── cookie-signature@1.0.6

morgan@1.6.1 node_modules/morgan
├── on-headers@1.0.1
├── basic-auth@1.0.3
├── depd@1.0.1
└── on-finished@2.3.0 (ee-first@1.1.1)

body-parser@1.13.3 node_modules/body-parser
├── bytes@2.1.0
├── content-type@1.0.1
├── depd@1.0.1
├── qs@4.0.0
├── http-errors@1.3.1 (inherits@2.0.1, statuses@1.2.1)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)
├── iconv-lite@0.4.11
└── raw-body@2.1.4 (unpipe@1.0.0, iconv-lite@0.4.12)

express@4.13.3 node_modules/express
├── escape-html@1.0.2
├── merge-descriptors@1.0.0
├── array-flatten@1.1.1
├── cookie@0.1.3
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.3.0
├── range-parser@1.0.2
├── vary@1.0.1
├── path-to-regexp@0.1.7
├── etag@1.7.0
├── content-type@1.0.1
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── serve-static@1.10.0
├── depd@1.0.1
├── finalhandler@0.4.0 (unpipe@1.0.0)
├── qs@4.0.0
├── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)
├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)
└── accepts@1.2.13 (negotiator@0.5.3, mime-types@2.1.7)

jade@1.11.0 node_modules/jade
├── character-parser@1.2.1
├── void-elements@2.0.1
├── commander@2.6.0
├── mkdirp@0.5.1 (minimist@0.0.8)
├── constantinople@3.0.2 (acorn@2.4.0)
├── jstransformer@0.0.2 (is-promise@2.1.0, promise@6.1.0)
├── with@4.0.3 (acorn@1.2.2, acorn-globals@1.0.6)
├── clean-css@3.4.6 (commander@2.8.1, source-map@0.4.4)
├── transformers@2.1.0 (promise@2.0.0, css@1.0.8, uglify-js@2.2.5)
└── uglify-js@2.5.0 (uglify-to-browserify@1.0.2, async@0.2.10, source-map@0.5.1, yargs@3.5.4)

經過 Express 應用生成器建立的應用通常都有以下目錄結構:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

7 directories, 9 files

 

7.啓動服務

MacOS 或 Linux 平臺:

$ DEBUG=myapp npm start

Windows 平臺:

> set DEBUG=myapp & npm start

 

至此,一個簡單的服務器已經搭建完成,並啓動了服務,在瀏覽器中打開http://localhost:3000/網址就能夠看到了。

詳細參考Express官網:http://www.expressjs.com.cn/

相關文章
相關標籤/搜索