當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。