Express Api总结

Express 主要Api总结

express

express.json() //上传body类型为"application/json",将请求的body内容放到request.body上(等同于request监听data事件拿到chunk后做格式化)
express.raw() //上传body类型为"application/json"
express.urlencoded() //上传body类型为"application/json"
express.text() //上传body类型为"application/octet-stream"
express.static() //将某个目录作为静态文件目录
express.Router()创建路由对象

app

app.locals //可以赋值到这个对象上
app.get()
app.set() //设置该应用的一些配置如:case sensitive routing区分大小写,etag过期时间,views模板目录,view engine模板引擎
app.METHOD(url,(req,res,next)=>{})
app.use(fn)

Request

req.app //该应用实例对象
req.range(size[, options]) //
req.get(‘Content-Type’) //text/plain

Response

res.append() //设置头
res.get() //获取头
res.set() //设置头
res.send([body]) //返回非流响应
res.end()

res.format{
  'text/plain': function () {
    res.send('hey')
  },
  'default': function () {
    // log the request and respond with 406
    res.status(406).send('Not Acceptable')
  }
})

Router

const router = express.Router()
router.route(path).all(function (req, res, next) {
    next()
  }).get(function (req, res, next) {
    res.json(req.user)
  })
  router.use('/bar', function (req, res, next) {
  // ... maybe some additional /bar logging ...
  next()
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值