本文整理匯總了TypeScript中koa-bodyparser類的典型用法代碼示例。如果您正苦於以下問題:TypeScript koa-bodyparser類的具體用法?TypeScript koa-bodyparser怎麽用?TypeScript koa-bodyparser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了koa-bodyparser類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: next
import * as jwt from 'jsonwebtoken';
import * as mount from 'koa-mount';
import * as responseTime from 'koa-response-time';
import { UserService } from './routes/users/user.service';
import { BindRoutes } from './routes';
const APP_CONFIG = config.get('app');
const JWT_CONFIG = config.get('jwt');
const app = new Koa();
const userService = new UserService();
// X-Response-Time
app.use(responseTime());
// Body Parsing
app.use(bodyparser());
// 404 Handler
app.use(async (ctx, next) => {
await next();
if (ctx.status === 404) {
ctx.response.status = 404;
ctx.response.body = {
error: true,
message: 'Entity not found'
};
}
});
// 401 Handler
app.use(async (ctx, next) => {
示例2: next
rolling: false,
renew: false,
};
app.keys = ["some secret hurr"];
// 錯誤處理
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
errorHandler(ctx, next, err);
}
});
app.use(bodyParse());
app.use(session(sessionConfig, app));
// app.use((ctx, next) => {
// const userName = ctx.session.user || "";
// });
app.use(apiRouters.routes()).use(apiRouters.allowedMethods());
app.use((ctx, next) => {
// 根據狀態碼拋出相應錯誤
const statusCode = ctx.response.status;
switch (statusCode) {
case 404:
throw new ResourceNotFoundError(ctx.request.method, ctx.request.url, "沒有找到您所需要的資源");
示例3: Koa
import * as serve from 'koa-static';
import * as cors from 'koa2-cors';
import * as path from 'path';
import rank from './routes/rank';
import news from './routes/news';
import analytics from './routes/analytics';
import summarize from './routes/summarize';
import { crawlJob } from './database/crawler';
const app = new Koa();
const port = process.env.PORT || 3000;
const dist = path.join(__dirname, '..', 'public');
const bpOption = { extendTypes: { json: ['application/x-javascript'] } };
app
.use(koaLogger())
.use(bodyParser(bpOption))
.use(cors())
.use(rank.routes())
.use(analytics.routes())
.use(news.routes())
.use(summarize.routes())
.use(serve(dist));
if (process.argv[2] === '--with-crawler') {
crawlJob.start();
}
app.listen(port, () => logger.info(`Listening on PORT ${port}`));