当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript koa.callback函数代码示例

本文整理汇总了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());
	}
}
开发者ID:ha-dai,项目名称:Misskey,代码行数:12,代码来源:index.ts

示例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();
});
开发者ID:komapijs,项目名称:komapi,代码行数:30,代码来源:errorHandler.ts

示例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();
});
开发者ID:komapijs,项目名称:komapi,代码行数:25,代码来源:requestLogger.ts

示例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`)
    })
})
开发者ID:YimYijet,项目名称:WebTest,代码行数:7,代码来源:index.ts

示例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();
});
开发者ID:komapijs,项目名称:komapi,代码行数:26,代码来源:ensureSchema.ts


注:本文中的koa.callback函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。