當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript kcors類代碼示例

本文整理匯總了TypeScript中kcors的典型用法代碼示例。如果您正苦於以下問題:TypeScript kcors類的具體用法?TypeScript kcors怎麽用?TypeScript kcors使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了kcors類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: next

const app = new Koa()
let appAny: any = app
appAny.counter = { users: {}, mock: 0 }

app.keys = config.keys
app.use(session(config.session, app))
if (process.env.NODE_ENV === 'development') app.use(logger())
app.use(async(ctx, next) => {
  await next()
  if (ctx.path === '/favicon.ico') return
  ctx.session.views = (ctx.session.views || 0) + 1
  let app: any = ctx.app
  if (ctx.session.fullname) app.counter.users[ctx.session.fullname] = true
})
app.use(cors({
  credentials: true
}))
app.use(async(ctx, next) => {
  await next()
  if (typeof ctx.body === 'object' && ctx.body.data !== undefined) {
    ctx.type = 'json'
    // ctx.body.path = ctx.path
    ctx.body = JSON.stringify(ctx.body, undefined, 2)
  }
})
app.use(async(ctx, next) => {
  await next()
  if (ctx.request.query.callback) {
    let body = typeof ctx.body === 'object' ? JSON.stringify(ctx.body, undefined, 2) : ctx.body
    ctx.body = ctx.request.query.callback + '(' + body + ')'
    ctx.type = 'application/x-javascript'
開發者ID:zerolugithub,項目名稱:rap2-delos,代碼行數:31,代碼來源:app.ts

示例2:

})
router.put('/data', (ctx: Koa.Context) => {
    ctx.set('my-header', 'hello')
    ctx.body = {
        content: {},
        code: 200
    }
})

app.use(cors({
    origin: (ctx: Koa.Context) => {
        const whiteList = ['http://localhost:3000']
        if (whiteList.includes(ctx.header.origin)) {
            return ctx.header.origin
        }
        return ''
    },
    exposeHeaders: 'my-header',
    allowMethods: 'PUT',
    allowHeaders: 'req-header',
    maxAge: 6,
    credentials: true    
}))

// app.use((ctx: Koa.Context, next: () => void) => {
//     const whiteList = ['http://localhost:3000']
//     if (whiteList.includes(ctx.header.origin)) {
//         // 來源
//         ctx.set('Access-Control-Allow-Origin', ctx.header.origin)
//         // 前端安全header
//         ctx.set('Access-Control-Expose-Headers', 'my-header')
//         // 允許cookie
開發者ID:YimYijet,項目名稱:WebTest,代碼行數:32,代碼來源:cors.ts

示例3: sanitizeRoom

}

function sanitizeRoom({ name, memberCount }: any) {
  return {
    name,
    memberCount,
  }
}

const router = new KoaRouter()

router.get("/", async (ctx, next) => {
  ctx.body = board.rooms.chain().simplesort("memberCount", true).data().map(sanitizeRoom)
})

app.use(cors())
app.use(router.routes()).use(router.allowedMethods())

// server.on("request", function (req, res) {
//   if (req.url === "/") {
//     res.writeHead(302, {
//       Location: "https://github.com/rtc-io/rtc-switchboard",
//     })
//     res.end("switchboard available from: https://github.com/rtc-io/rtc-switchboard")
//   }
//   if (req.url === "/rooms") {
//     res.end(JSON.stringify(board.rooms.chain().simplesort("memberCount", true).data().map(sanitizeRoom)))
//   }
// })

// start the server
開發者ID:bengt-games,項目名稱:curves-server,代碼行數:31,代碼來源:server.ts

示例4: next

}

// Router
const router = new KoaRouter()
  .get("/ping", async ctx => {
    ctx.body = "pong";
  })
  .get("/favicon.ico", async ctx => {
    ctx.status = 204;
  });

// Koa instance
export const app = new Koa()

  // CORS
  .use(koaCors())

  // Error catcher
  .use(async (ctx, next) => {
    try {
      await next();
    } catch (e) {
      console.log("Error:", e);
      ctx.status = 500;
      ctx.body = "There was an error. Please try again later.";
    }
  })

  // Timing
  .use(async (ctx, next) => {
    const start = ms.now();
開發者ID:leebenson,項目名稱:cli,代碼行數:31,代碼來源:app.ts


注:本文中的kcors類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。