本文整理匯總了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();
});