KOA 中间件 入门

来源:互联网 发布:福建广电网络集团班子 编辑:程序博客网 时间:2024/05/04 22:29
实现如果请求的url 中带有 callback 则按照jsonp 格式返回
http://localhost:3000/?callback=abc
var koa      = require('koa');var app = koa();app.use(function *(next) {    yield next;    var callback = this.query["callback"];    if (!callback) return    this.type = 'text/javascript'    startChunk =  callback + '('    endChunk = ');'    this.body =  startChunk + JSON.stringify(this.body) + endChunk;});app.use(function *() {        this.body = {"username":"xixi"};    })app.listen(3000,function(){    console.log('now listening on port 3000');});
                                             
0 0
原创粉丝点击