视频地址:www.cctalk.com/v/151149238…html
我颠倒了整个世界,只为摆正你的倒影。json
前面的文章中,咱们已经完成了项目中常见的问题,好比 路由请求
、结构分层
、视图渲染
、静态资源
等。 那么,JSON
呢?JSON
格式数据的传输,已经深刻到了咱们的码里行间,脱离了 JSON
的人想必是痛苦的。那么,复合吧!小程序
伟大的武术家——李小龙先生——说过这样一段话:微信小程序
Empty your mind, Be formless,shapeless like water.
You put water in a cup, it becomes the cup.
You put water in a bottle, it becomes the bottle.
You put water in a teapot , it becomes the teapot.
Water can flow or crash.
复制代码
翻译成中文意思就是:微信
清空你的思想,像水同样无形。
你将水倒入水杯,水就是水杯的形状。
你将水倒入瓶子,水就是瓶子的形状。
你将水倒入茶壶,水就是茶壶的形状。
你看,水会流动,也会冲击。
复制代码
在数据传输过程当中,传输的资源均可以称之为『数据』,而『数据』之因此展现出不一样的形态,是由于咱们已经设置了它的格式。app
传输的数据像是『水』同样,没有任何的格式和形状。less
咱们的设置像是『器』同样,赋予它指定的形态。koa
因此,咱们只须要设置把数据挂载在响应体 body
上,同时告诉客户端『返回的是 JSON
数据』,客户端就会按照 JSON
来解析了。代码以下:async
ctx.set("Content-Type", "application/json")
ctx.body = JSON.stringify(json)
复制代码
咱们把上面的代码提取成一个中间件,这样更方便代码的维护性和扩展性post
增长文件 /middleware/mi-send/index.js
:
module.exports = () => {
function render(json) {
this.set("Content-Type", "application/json")
this.body = JSON.stringify(json)
}
return async (ctx, next) => {
ctx.send = render.bind(ctx)
await next()
}
}
复制代码
注意: 目录不存在,须要本身建立。
代码中,咱们把 JSON
数据的处理方法挂载在 ctx
对象中,并起名为 send
。当咱们须要返回 JSON
数据给客户端时候,只须要调用此方法,并把 JSON
对象做为参数传入到方法中就好了,用法以下:
ctx.send({
status: 'success',
data: 'hello ikcmap'
})
复制代码
代码的实现过程和调用方法咱们已经知道了,如今咱们须要把这个中间件应用在项目中。
middleware/index.js
,用来集中调用全部的中间件:const miSend = require('./mi-send')
module.exports = (app) => {
app.use(miSend())
}
复制代码
app.js
,增长中间件的引用const Koa = require('koa')
const path = require('path')
const bodyParser = require('koa-bodyparser')
const nunjucks = require('koa-nunjucks-2')
const staticFiles = require('koa-static')
const app = new Koa()
const router = require('./router')
const middleware = require('./middleware')
middleware(app)
app.use(staticFiles(path.resolve(__dirname, "./public")))
app.use(nunjucks({
ext: 'html',
path: path.join(__dirname, 'views'),
nunjucksConfig: {
trimBlocks: true
}
}));
app.use(bodyParser())
router(app)
app.listen(3000, () => {
console.log('server is running at http://localhost:3000')
})
复制代码
随着项目的步步完善,将会产生有更多的中间件。咱们把 app.js
中的中间件代码迁移到 middleware/index.js
中,方便后期维护扩展
app.js
const Koa = require('koa')
const app = new Koa()
const router = require('./router')
const middleware = require('./middleware')
middleware(app)
router(app)
app.listen(3000, () => {
console.log('server is running at http://localhost:3000')
})
复制代码
middleware/index.js
const path = require('path')
const bodyParser = require('koa-bodyparser')
const nunjucks = require('koa-nunjucks-2')
const staticFiles = require('koa-static')
const miSend = require('./mi-send')
module.exports = (app) => {
app.use(staticFiles(path.resolve(__dirname, "../public")))
app.use(nunjucks({
ext: 'html',
path: path.join(__dirname, '../views'),
nunjucksConfig: {
trimBlocks: true
}
}));
app.use(bodyParser())
app.use(miSend())
}
复制代码
后面咱们还会开发更多的中间件,好比日志记录、错误处理等,都会放在 middleware/
目录下处理。
下一篇:记录日志——开发日志中间件,记录项目中的各类形式信息
上一篇:iKcamp新课程推出啦~~~~~iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 处理静态资源
2019年,iKcamp原创新书《Koa与Node.js开发实战》已在京东、天猫、亚马逊、当当开售啦!