这是用于 Node.js 的快速、不拘一格、极简主义的 Web 框架,包含 Express.js 的 API 参考列表和一些示例。
创建项目,添加 package.json 配置
$ mkdir myapp # 创建目录
$ cd myapp    # 进入目录
$ npm init -y # 初始化一个配置
安装依赖
$ npm install express # 安装依赖
入口文件 index.js 添加代码:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
  res.send('Hello World!')
})
app.listen(port, () => {
  console.log(`监听端口${port}示例应用`)
})
使用以下命令运行应用程序
$ node index.js
Usage: express [options] [dir]
Options:
  -h, --help          输出使用信息
      --version       输出版本号
  -e, --ejs           添加 ejs 引擎支持
      --hbs           添加 hbs 引擎支持
      --pug           添加 pug 引擎支持
  -H, --hogan         添加 hogan.js 引擎支持
      --no-view       无视图引擎生成
  -v, --view <engine> 添加视图 <engine> 支持 (ejs|hbs|hjs|jade|pug|twig|vash) (默认jade) 
  -c, --css <engine>  添加样式表 <engine> 支持 (less|stylus|compass|sass) (默认css)
      --git           添加 .gitignore
  -f, --force         强制非空目录
创建一个 myapp 的项目
$ express --view=pug myapp
# 运行应用程序
$ DEBUG=myapp:* npm start
var express = require('express')
var app = express()
console.dir(app.locals.title)
// => 'My App'
console.dir(app.locals.email)
// => 'me@myapp.com'
| :- | :- | 
|---|---|
| app.locals | 应用程序中的局部变量 # | 
| app.mountpath | 安装子应用程序的路径模式 # | 
| :- | :- | 
|---|---|
| mount | 子应用挂载到父应用上,子应用上触发事件 # | 
| :- | :- | 
|---|---|
| app.all() | # | 
| app.delete() | # | 
| app.disable() | # | 
| app.disabled() | # | 
| app.enable() | # | 
| app.enabled() | # | 
| app.engine() | # | 
| app.get(name) | # | 
| app.get(path, callback) | # | 
| app.listen() | # | 
| app.METHOD() | # | 
| app.param() | # | 
| app.path() | # | 
| app.post() | # | 
| app.put() | # | 
| app.render() | # | 
| app.route() | # | 
| app.set() | # | 
| app.use() | # | 
| :- | :- | 
|---|---|
| req.app | # | 
| req.baseUrl | # | 
| req.body | # | 
| req.cookies | # | 
| req.fresh | # | 
| req.hostname | # | 
| req.ip | # | 
| req.ips | # | 
| req.method | # | 
| req.originalUrl | # | 
| req.params | # | 
| req.path | # | 
| req.protocol | # | 
| req.query | # | 
| req.route | # | 
| req.secure | # | 
| req.signedCookies | # | 
| req.stale | # | 
| req.subdomains | # | 
| req.xhr | # | 
| :- | :- | 
|---|---|
| req.accepts() | # | 
| req.acceptsCharsets() | # | 
| req.acceptsEncodings() | # | 
| req.acceptsLanguages() | # | 
| req.get() | 获取HTTP 请求头字段 # | 
| req.is() | # | 
| req.param() | # | 
| req.range() | # | 
app.get('/', function (req, res) {
  console.dir(res.headersSent) // false
  res.send('OK')
  console.dir(res.headersSent) // true
})
| :- | :- | 
|---|---|
| res.app | # | 
| res.headersSent | # | 
| res.locals | # | 
| :- | :- | 
|---|---|
| res.append() | # | 
| res.attachment() | # | 
| res.cookie() | # | 
| res.clearCookie() | # | 
| res.download() | 提示要下载的文件 # | 
| res.end() | 结束响应过程 # | 
| res.format() | # | 
| res.get() | # | 
| res.json() | 发送 JSON 响应 # | 
| res.jsonp() | 发送带有 JSONP 支持的响应 # | 
| res.links() | # | 
| res.location() | # | 
| res.redirect() | 重定向请求 # | 
| res.render() | 渲染视图模板 # | 
| res.send() | 发送各种类型的响应 # | 
| res.sendFile() | 将文件作为八位字节流发送 # | 
| res.sendStatus() | # | 
| res.set() | # | 
| res.status() | # | 
| res.type() | # | 
| res.vary() | # | 
为传递给此路由器的任何请求调用
router.use(function (req, res, next) {
  // .. 这里有一些逻辑 .. 像任何其他中间件一样
  next()
})
将处理任何以 /events 结尾的请求
// 取决于路由器在哪里 "use()"
router.get('/events', (req, res, next) => {
  // ..
})
res 对象表示 Express 应用程序在收到 HTTP 请求时发送的 HTTP 响应
app.get('/user/:id', (req, res) => {
  res.send('user ' + req.params.id)
})
req 对象表示 HTTP 请求,并具有请求查询字符串、参数、正文、HTTP 标头等的属性
app.get('/user/:id', (req, res) => {
  res.send('user ' + req.params.id)
})
res.end()
res.status(404).end()
结束响应过程。这个方法其实来自 Node 核心,具体是 http.ServerResponse 的 response.end() 方法
res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })
app.all('/secret', function (req, res, next) {
  console.log('访问秘密部分...')
  next() // 将控制权传递给下一个处理程序
})
app.delete('/', function (req, res) {
  res.send('DELETE request to homepage')
})
app.disable('trust proxy')
app.get('trust proxy')
// => false
app.disabled('trust proxy')
// => true
app.enable('trust proxy')
app.disabled('trust proxy')
// => false
var engines = require('consolidate')
app.engine('haml', engines.haml)
app.engine('html', engines.hogan)
var express = require('express')
var app = express()
app.listen(3000)
const express = require('express')
const app = express()
// 向主页发出 GET 请求时响应“hello world”
app.get('/', (req, res) => {
  res.send('hello world')
})
// GET 方法路由
app.get('/', (req, res) => {
  res.send('GET request to the homepage')
})
// POST 方法路由
app.post('/', (req, res) => {
  res.send('POST request to the homepage')
})
function logOriginalUrl (req, res, next) {
  console.log('ReqURL:', req.originalUrl)
  next()
}
function logMethod (req, res, next) {
  console.log('Request Type:', req.method)
  next()
}
const log = [logOriginalUrl, logMethod]
app.get('/user/:id', log,
  (req, res, next)=>{
    res.send('User Info')
  }
)
app.set('view engine', 'pug')
在 views 目录下创建一个名为 index.pug 的 Pug 模板文件,内容如下
html
  head
    title= title
  body
    h1= message
创建一个路由来渲染 index.pug 文件。如果未设置视图引擎属性,则必须指定视图文件的扩展名
app.get('/', (req, res) => {
  res.render('index', {
    title: 'Hey', message: 'Hello there!'
  })
})