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