本文整理汇总了TypeScript中koa.callback函数的典型用法代码示例。如果您正苦于以下问题:TypeScript callback函数的具体用法?TypeScript callback怎么用?TypeScript callback使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了callback函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createServer
function createServer() {
if (config.https) {
const certs: any = {};
Object.keys(config.https).forEach(k => {
certs[k] = fs.readFileSync(config.https[k]);
});
certs['allowHTTP1'] = true;
return http2.createSecureServer(certs, app.callback());
} else {
return http.createServer(app.callback());
}
}
示例2: it
it('should gracefully fail if provided a non-error', async done => {
expect.assertions(2);
const app = new Koa();
class InvalidError {}
// Add middlewares
app.use(errorHandler());
app.use(() => {
throw new InvalidError();
});
const response = await request(app.callback()).get('/');
// Assertions
expect(response.status).toBe(500);
expect(response.body).toEqual({
error: {
additionalDevelopmentData: {
stack: expect.stringContaining('Error: Cannot handle non-errors as errors'),
},
code: '',
error: 'Internal Server Error',
message: 'An internal server error occurred',
status: 500,
},
});
// Done
done();
});
示例3: it
it('should log latency in milliseconds', async done => {
expect.assertions(3);
const app = new Koa();
const spy = jest.fn();
// Add middlewares
app.use((ctx, next) => {
// Add mock data
ctx.request.startAt = Date.now();
ctx.request.log = { info: spy } as any;
return next();
});
app.use(requestLogger());
app.use((ctx, next) => new Promise(resolve => setTimeout(resolve, 125)).then(next));
const response = await request(app.callback()).get('/');
// Assertions
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0].latency).toBeGreaterThan(120);
expect(spy.mock.calls[0][0].latency).toBeLessThan(1200);
// Done
done();
});
示例4: connectDB
connectDB().then(() => {
console.log('database connected')
app.use(router.routes())
http.createServer(app.callback()).listen(4000, () => {
console.log(`http server listening on port: 4000`)
})
})
示例5: it
it('should reject invalid data and stop request cycle', async done => {
expect.assertions(2);
const app = new Koa();
const data = {
id: 1,
name: 'John Smith',
comment: 'A long comment about this generic person',
};
// Add middlewares
app.use((ctx, next) => {
(ctx.request as any).body = data; // Hack to inject a body
return next();
});
app.use(ensureSchema(exampleSchema));
app.use(ctx => fail('should not continue requests'));
const response = await request(app.callback()).get('/');
// Assertions
expect(response.status).toBe(500);
expect(response.text).toEqual('Internal Server Error');
// Done
done();
});