本文整理汇总了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'
示例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
示例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
示例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();