本文整理汇总了TypeScript中koa.on函数的典型用法代码示例。如果您正苦于以下问题:TypeScript on函数的具体用法?TypeScript on怎么用?TypeScript on使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了on函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: beforeAll
beforeAll((done)=> {
jasmine.DEFAULT_TIMEOUT_INTERVAL=1000;
const Koa = require('koa');
const busboy = new KoaBusBoy({
limits: {
fields: 1,
fileSize: 1
}
});
const app = new Koa();
app.use(busboy);
app.use((ctx: any)=> {
if (ctx.formData) {
ctx.body = ctx.formData;
} else {
ctx.body = 'hello world';
}
});
app.on('error', (error: Error, ctx: any)=> {
ctx.status = 400;
ctx.body = error.message;
});
app.listen(3001, ()=> {
done();
});
});
示例2: Koa
const koa = new Koa();
const router = new Router();
const JWT_SECRET = "" + config.get("UI_JWT_SECRET");
const JWT_COOKIE = "genieacs-ui-jwt";
function getPermissionSets(ctx): PermissionSet[] {
const allPermissions = localCache.getPermissions(ctx.state.configSnapshot);
const permissionSets = ctx.state.user.roles.map(role =>
Object.values(allPermissions[role] || {})
);
return permissionSets;
}
koa.on("error", async err => {
throw err;
});
koa.use(async (ctx, next) => {
const configSnapshot = await localCache.getCurrentSnapshot();
ctx.state.configSnapshot = configSnapshot;
return next();
});
koa.use(
koaJwt({
secret: JWT_SECRET,
passthrough: true,
cookie: JWT_COOKIE
})
);
示例3: next
app
.use(koaBodyparser())
.use(async (ctx: Koa.Context, next: any) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
app.emit("error", err, ctx);
}
})
.use(github)
.use(slackMiddleware);
app.on("error", err => {
log(err);
});
app.on("ready", msg => {
log(msg);
});
if (!module.parent) {
app.listen(port, () =>
app.emit("ready", chalk.black.bgGreen.bold(`Listening on port ${port}`))
);
}
export default app;
示例4: next
viewPath: viewPath,
noCache: Config.isDevEnv,
pretty: Config.isDevEnv,
locals: {
iconsDir: '/dist/public/images/icons'
}
});
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
if (error instanceof MongoError) {
log('Got unhandled mongo error, checking connection.');
rebuildConnectionAsync();
} else {
log(`Error on middleware\n${error}`);
}
ctx.body = { message: error.message };
ctx.status = error.status || 500;
}
});
app.use(router.routes())
.use(router.allowedMethods());
app.on('error', (err: any, ctx: any) => {
console.error('server error', err, ctx);
});
export default app;
示例5: switch
switch (environment) {
case 'build':
//TODO: implement build environment server
break;
default:
console.log('*** DEV ***');
console.log('rootDir: ' + rootDir);
app.use(convert(serve(path.join(rootDir, './src/client'))));
app.use(convert(serve(path.join(rootDir, './'), { hidden: true })));
router.get('/*', function *(ctx: KoaRouter.IRouterContext, next: any) {
this.body = yield fs.readFile(path.join(rootDir, './src/client/index.html'), 'utf-8');
});
break;
}
app.use(convert(router.routes()));
app.on('error', (err: Error) => {
console.log(err);
console.log('Koa server error occurred.');
});
if (!module.parent) {
app.listen(port, function() {
console.log(`Koa server listening on port ${port}...`);
});
}