koa2 知识点

  • koa2 常用功能:
    假设 Koa 提供的 Context 对象的实例为 ctx
语句 作用
ctx.response.body 发送给用户的内容
ctx.request.accepts 判断客户端希望接收数据的类型
ctx.response.type 指定返回给客户端数据的类型
fs.createReadStream 读取网页模板
ctx.request.path 获取用户请求的路径
ctx.response.redirect 发出一个302跳转,将用户导向另一个路由
ctx.throw 抛出错误,无法定制错误提示信息: ctx.throw(500)
ctx.response.status 跟ctx.response.body配合使用,可提供定制的错误提示,例如:ctx.response.status = 404; ctx.response.body = 'Page not found'
ctx.cookies.get 读 Cookie
ctx.cookies.set 写 Cookie
  • "中间件"(middleware):
    因为它处在 HTTP Request 和 HTTP Response 中间,用来实现某种中间功能。

  • 运行 npm run dev 命令报错:

This dependency was not found:
fs in ./~/destroy/index.js
To install it, you can run: npm install --save fs

原因:
前端用了后端的库。我遇到过,在react里引入require('NET') net是服务端的api。所以报错。
net库和fs库都是chrome的v8引擎之外的写给nodejs用的。是用在服务端,而不是浏览器你的前端,你在前端渲染,肯定就加载不了这个模块了。

原文地址:https://www.cnblogs.com/cag2050/p/7526959.html