使用Express搭建服务器

Express是基于Node.js平台,快速、开放、极简的web开发框架。所以,使用Express之前,请确保已安装Node.js

1.创建一个目录作为当前工作目录:

$ mkdir myapp
$ cd myapp

 

2.通过npm  init命令为你的应用创建一个package.json文件。欲了解package.json是如何起作用的,请参考 Specifics of npm’s package.json handling。此命令将要求你输入几个参数,例如应用的名称和版本。可直接按“回车”键接受默认设置。

此命令会在当前目录下生成package.json文件。

$ 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.

See `npm help json` for definitive documentation on 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 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:

{
"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)

敲了这么多回车,让我们看一下这个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并将其保存到依赖列表中:

 

4.使用express生成器创建一个应用的骨架

$ npm install express-generator -g

[email protected] /usr/local/lib/node_modules/express-generator
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected])

运行$ 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

[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])

通过 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/

 

posted @ 2015-10-15 14:49  -nothing-  阅读(1840)  评论(0)    收藏  举报