Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 544 Bytes

koa-jsonp中间件.md

File metadata and controls

34 lines (25 loc) · 544 Bytes

koa-jsonp中间件

koa.js 官方wiki中也介绍了不少jsonp的中间件

安装

npm install --save koa-jsonp

const Koa = require('koa')
const jsonp = require('koa-jsonp')
const app = new Koa()

// 使用中间件
app.use(jsonp())

app.use( async ( ctx ) => {

  let returnData = {
    success: true,
    data: {
      text: 'this is a jsonp api',
      time: new Date().getTime(),
    }
  }

  // 直接输出JSON
  ctx.body = returnData
})

app.listen(3000, () => {
  console.log('[demo] jsonp is starting at port 3000')
})