本文整理汇总了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}`));