express

express

  • var express = require('express')
  • var app = express();生成一个express程序

express.static()

  • express.static唯一内建中间件(静态资源)

application

  • app.get(path,callback)
  • app.listen(端口号);
  • app.locals

    • 各个属性是在整个程序内有效,如:app.locals.title app.locals.email
    • res.locals只在本次请求内有效
  • app.mountpath属性

    • 是子程序挂载的路径模式(列表)。
  • app.on('mount',callback(parent){})

    • 当子程序被挂载到父程序时,mount事件会被发射,父程序对象作为参传进来.
  • app.all(path,callback[,callback])

    • 所有符合路径的都要执行一遍
    • *表示所有
  • app.delete(path,callback[,callback])

    • HTTP DELETE请求时
  • app.disable(属性)

    • 将这个布尔属性置为false,属性为app settings table中之一
    • app.enable(属性),置为真,
    • app.disabled(属性)判断是否为假
    • app.enabled(属性)判断是否为真
  • app.engine('jade等',callback)

    • 注册给定引擎的回调,用来渲染处理ext文件
  • app.get(属性)

    • 获得APP的属性的值,为app settings table中之一
  • app.get(path,callback)

    • important
  • app.listen(port, [hostname], [backlog], [callback])

    • 绑定程序监听端口到指定的主机和端口号
  • app.param([name],callback)

    • 路由路径中,:name被定义为参数
    • name参数名或者数组,callback5个参数,请求,响应,下个中间价,参数值,参数名
    • 首先执行,只有一(数组值个)次
  • app.path()

    • 获取当前的指定路径,String类型
  • app.render(view,[locals],callback)

  • app.route(path)

    • app.route(path).all(callback).get(callback)
    • 避免一路径多路由
  • app.set(name,value)

    • 给属性赋值
    • name为app settings table中的值
  •  app.use(path,callback)

    • 顺序处理
    • 用于预处理

 request

  • req.app

    • 可当做中间件的一个express实例
    • app主文件
    • req.app副文件
  • req.baseUrl

    • 一个路由实例挂载的Url路径
  •  req.body

    • 在请求的body中保存的一组组键值对,默认undefined
    • 当使用body-parser和multer等解析中间件时,他是填充的
      eg:
      var app = require('express');
      var bodyParser = require('body-parser');
      var multer = require('multer');
      var upload = multer();
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({extend:true}));
      app.post('/profile',upload.array(),callback)

       

  • req.cookies

    • 使用cooki-parser中间件解析
    • 这是一个对象,包含了信息,没有则为{}
  • req.signedCookies

    • 使用cooki-parser中间件
    • 获取带签名的cookies,没有默认为{}
  • req.fresh

  • req.stale

  • req.hostname

  • req.ips

  • req.originalUrl

  • req.params

    • 和路由中参数一一对应的一个对象
    • 默认{}
  • req.path

    • 包含url的部分路径
  • req.protocol

    • 请求的协议
  • req.query

    • //GET/search?q=tobi+ferret
      req.query.q// tobi ferret
  • req.route

    • 当前匹配的路由,为一串字符串
  • req.secure

  • req.subdomains

    • 请求域名的子域名数组
    • eg:
      //tobi.ferrets.example.com
      [ferrets,tobi]
  • req.xhr

    • 布尔值
  • req.accepts(types)

  • req.acceptsCharsets(charset[, ...])

  • req.acceptsEncodings(encoding[, ...])

  • req.acceptsLanguages(lang [, ...])

  • req.get(field)      req.header(field)

    • 返回指定的请求http头部中你想要的内容
  • req.is(TYPE)

    • 布尔,判断你请求的Content-type类型与给定的TYPE

 response

  • res.app

    • express实例和res.app属性相同
  • res.headersSent

    • 布尔类型,判断是否send过
  • res.local

    • 对象,只在本次响应中有效(可供试图渲染)
    • 其他类似app.locals

   方法

  • res.append(field,[value])

    • 指定头部域中追加特殊值,如无可新建
    • app.set()会重置之前的
  • res.attachment([filename])

    • Content-Type设置成attachment
    • 有文件名,则filename也命名之
    • res.attachment()
      //Content-Disposition:attachment
      res,attachment(path/to/logo.png)
      //Content-Disposition:attachment;filename="logo.png"
      //Content-Type:image/png
  •  res.cookie(name,value,[options])

    • value可以是一串字符或者json
    • options对象
  • res.clearCookie(name,[options]) 

  • res.download(path,[filename],[callback])

    • path文件路径
    • 其用res.sendfile()传输文件
    • res.download('test.txt')
      
      res.download('test.txt','new.txt')
      
      res.download('test.txt','new.txt',callback)
  • res.end([data],[encoding])

    • 结束响应
  • res.format(object)

  • res.get(field)

    • field是http头部
    • res.get('Content-Type');
      //"text/plain"
  •  res.json([body])

    • 将传入的对象或数组像res.send()
    • 特殊值如null,undefined也会被处理而send不会
  • res.jsonp() 

  • res.links(links)

    • res.links({
      next:'http://api.example.com/users?page=2',
      last:'http://api.example.com/users?page=5'
      });
      //
      Link:<http://api.example.com/users?page=2>;rel="next",
         <http://api.example.com/users?page=5>;rel="last"
    • 连接links,内容用来填充响应的http头部
  • res.location(path)

    • 设置响应的头部为指定的path
    • 当path为back时,其为请求对象的引用的url,没有为空
  •  res.redirect([status],path)

    • 重定向状态(可选)和路径
    • /admin主机根   image/image当前  back 注意请求的尾部/
  •  res.render(view,[locals],[callback])

    • 渲染一个模板,并发送给客户端
    • locals视图本地属性的一个对象
    • 回调函数两个参数err和html,如果存在则不发送回客户端,需自己send
  •  res.send([body])

    • body可以是Buffer,字符串,对象,数组
    • //自动赋值
                 Content-Type       
      Buffer       application/octet-stream
      字符串        text/html
      对象或数组     json格式
  •  res.sendFile(path,[options],[callback])

    • ..
  •  res.sendStatus(statuscode)

    • 设置http status code并将对应的信息设为响应的body
    • res.sendStatus(200); // equivalent to res.status(200).send('OK');
      res.sendStatus(403); // equivalent to res.status(403).send('Forbidden');
      res.sendStatus(404); // equivalent to res.status(404).send('Not Found');
      res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
      特殊:res.sendStatus(2000);//equivalent to res.status(2000).send('2000')
  •  res.set(field,[value])    res.header(field,[value])

    • 设置头部内容
    • res.set('Content-Type', 'text/plain');
      res.set({
          'Content-Type':'text/plain',
          'Content-Length':'123',
          'ETag':'123456'
      })
  •  res.status(code)

    • 设置http状态
  •  res.type(type)

    • 设置Content-TypeHTTP头的MIME type,如果type能够被mime.lookup解析成正确的Content-Type。如果type中包含了/字符,那么程序会直接设置Content-Type为type。
    • res.type('.html');              // => 'text/html'
      res.type('html');               // => 'text/html'
      res.type('json');               // => 'application/json'
      res.type('application/json');   // => 'application/json'
      res.type('png');                // => image/png:
  •  res.vary(field)

    • 在没有vary应答头部的时候增加vary应答头部

Router

一个router对象是一个单独的实例关于中间件和路由,有操作中间件和路由方法的能力。每个Express程序有一个内建的app路由。
路由自身表现为一个中间件,所以你可以使用它作为app.use()方法的一个参数或者作为另一个路由的use()的参数。
顶层的express对象有一个Router()方法,你可以使用Router()来创建一个新的router对象。

  • Router[options]

    • var router = express.Router(options);
      //options
      //caseSensitive 是否区分大小写(默否)
      //mergeParams 保存父路由params
      //strict             使能严格路由
  • router.all(path,[callback,,])

  • router.MEDTHOD(path,[callback])

    • get()  post()  put()
    • 可通过next('router')绕过剩余的路由
  • router.parma(name,callback)

    • //路由出现 :user     一定要定义过的冒号后的
      router.param('user', function(req, res, next, id) {
              User.find(id, function(error, user) {
                  if (err) {
                      next(err);
                  }
                  else if (user){
                      req.user = user;
                  } else {
                      next(new Error('failed to load user'));
                  }
              });
          });
    • 路由参数的回调触发器,可用于验证,注:name不支持传入数组
    • 首先被调用,仅调用一次,局部的不能被中间件或路由继承
  •  router.router(path).other()

    • 单例模式的路由
    • router.route('/users/id')
          .all(function(req, res, next) {
              next();
          })
          .get(function(req, res, next) {
              res.json(req.user);
          })
  •  router.use(path,[callback])

    • 指定路径加载的中间件

 

posted @ 2018-01-12 21:36  王利群  阅读(347)  评论(0)    收藏  举报