本文整理汇总了TypeScript中koa-body类的典型用法代码示例。如果您正苦于以下问题:TypeScript koa-body类的具体用法?TypeScript koa-body怎么用?TypeScript koa-body使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了koa-body类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: constructor
md.remove(ctx.request.body.id);
ctx.body = true;
});
router.post('/edit', (ctx, next) => {
ctx.body = md.getFile(ctx.request.body.id);
});
router.post('/updateCont', (ctx, next) => {
md.setFile(ctx.request.body.id, ctx.request.body.cont);
ctx.body = true;
})
router.post('/updateTitle', (ctx, next) => {
const
id = ctx.request.body.id,
title = ctx.request.body.title;
ctx.body = md.setTitle(id, title);
});
koa
.use(body({}))
.use(router.routes())
.use(router.allowedMethods())
.use(Static(__dirname + '/view'))
.use(Static(path.join(__dirname ,'../../')));
export class Admin {
constructor(port) {
koa.listen(port, (e) => {
console.log(`Admin is run. port:${port}`);
// open('http://localhost:' + port);
});
}
}
示例2: next
if (ctx.path === '/favicon.ico') return
ctx.session.views = (ctx.session.views || 0) + 1
let app: any = ctx.app
if (ctx.session.fullname) app.counter.users[ctx.session.fullname] = true
})
app.use(cors({
credentials: true
}))
app.use(async(ctx, next) => {
await next()
if (typeof ctx.body === 'object' && ctx.body.data !== undefined) {
ctx.type = 'json'
// ctx.body.path = ctx.path
ctx.body = JSON.stringify(ctx.body, undefined, 2)
}
})
app.use(async(ctx, next) => {
await next()
if (ctx.request.query.callback) {
let body = typeof ctx.body === 'object' ? JSON.stringify(ctx.body, undefined, 2) : ctx.body
ctx.body = ctx.request.query.callback + '(' + body + ')'
ctx.type = 'application/x-javascript'
}
})
app.use(serve('public'))
app.use(serve('test'))
app.use(body())
app.use(router.routes())
export default app
示例3: saveVisitList
// record visit
if (whiteList[ctx.request.ip] === undefined) {
visitList[ctx.request.ip] = Date.now();
saveVisitList();
}
ctx.response.status = 200;
ctx.body = JSON.stringify({
statistics: { ...flavor },
code: 200
});
} catch (e) {
ctx.response.status = 400;
ctx.body = JSON.stringify(
{
message: e.message,
code: 400
},
null,
2
);
} finally {
fs.writeFileSync("./config.json", JSON.stringify(flavor, null, 2), "utf-8");
}
});
loadConfig();
app.use(Koabody());
app.use(router.routes()).listen(8080);
示例4: next
if (ctx.path === '/favicon.ico') return
ctx.session.views = (ctx.session.views || 0) + 1
let app: any = ctx.app
if (ctx.session.fullname) app.counter.users[ctx.session.fullname] = true
})
app.use(cors({
credentials: true,
}))
app.use(async (ctx, next) => {
await next()
if (typeof ctx.body === 'object' && ctx.body.data !== undefined) {
ctx.type = 'json'
ctx.body = JSON.stringify(ctx.body, undefined, 2)
}
})
app.use(async (ctx, next) => {
await next()
if (ctx.request.query.callback) {
let body = typeof ctx.body === 'object' ? JSON.stringify(ctx.body, undefined, 2) : ctx.body
ctx.body = ctx.request.query.callback + '(' + body + ')'
ctx.type = 'application/x-javascript'
}
})
app.use(serve('public'))
app.use(serve('test'))
app.use(bodyParser({ multipart: true }))
app.use(router.routes())
export default app
示例5: Body
async Body(ctx: Koa.IContext, next) {
await Convert(_Body())(ctx, next);
}
示例6: start
public start() {
if (this.server) {
return;
}
const self = this;
const app = koa();
app.use(function*(next: any): Iterator<void> {
this.header['content-type'] =
this.headers['content-type'] &&
this.headers['content-type'].replace(';charset=UTF-8', '').replace(';charset=utf-8', '');
yield next;
});
app.use(koaBody());
app.use(cors());
app.use(function*(): Iterator<void> {
const matched = self.mockedCalls.filter(
({method, pathRegex, queryParamsObject, bodyRestriction = {}}: MockedCall) => {
if (method !== this.req.method) {
return false;
}
if (!new RegExp(pathRegex).test(this.url)) {
return false;
}
const contentTypeIsApplicationJson = this.request.header['content-type'] === 'application/json';
if (queryParamsObject) {
const splitUrl = this.url.split('?');
if (splitUrl.length < 2) {
return false;
}
const queryParamsOnUrl = queryString.parse(splitUrl[1]);
if (!deepEquals(queryParamsOnUrl, queryParamsObject)) {
return false;
}
}
if (bodyRestriction.regex) {
const requestBodyAsString = contentTypeIsApplicationJson
? JSON.stringify(this.request.body)
: this.request.body;
if (!new RegExp(bodyRestriction.regex).test(requestBodyAsString)) {
return false;
}
}
if (
bodyRestriction.minimalObject &&
(!contentTypeIsApplicationJson || !isSubset(this.request.body, bodyRestriction.minimalObject))
) {
return false;
}
if (
bodyRestriction.object &&
(!contentTypeIsApplicationJson || !deepEquals(this.request.body, bodyRestriction.object))
) {
return false;
}
return true;
}
);
if (matched.length >= 1) {
self.callHistory.push({
method: this.req.method,
path: this.url,
headers: this.request.header,
body: this.request.body,
});
const firstMatch = matched[matched.length - 1];
self.logger(
`fakeServer:: call to [${this.req.method} ${this.url} ${JSON.stringify(
this.request.body
)}]. Respond with status: [${firstMatch.statusCode}] and body: [${JSON.stringify(
firstMatch.response
)}]`
);
this.status = firstMatch.statusCode;
this.body = firstMatch.response;
} else {
self.logger(
`fakeServer:: no match for [${this.req.method} ${this.url} ${JSON.stringify(this.request.body)}]`
);
this.status = 400;
this.body = `no match for [${this.req.method} ${this.url} ${JSON.stringify(this.request.body)}]`;
}
});
this.clearCallHistory();
this.server = !this.tls
//.........这里部分代码省略.........