當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript koa-body類代碼示例

本文整理匯總了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);
        });
    }
}
開發者ID:zoeDylan,項目名稱:zoeDylan.github.io,代碼行數:31,代碼來源:index.ts

示例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
開發者ID:zerolugithub,項目名稱:rap2-delos,代碼行數:31,代碼來源:app.ts

示例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);
開發者ID:TeamCovertDragon,項目名稱:duanwu-statistics,代碼行數:29,代碼來源:index.ts

示例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
開發者ID:tonyjt,項目名稱:rap2-delos,代碼行數:31,代碼來源:app.ts

示例5: Body

 async Body(ctx: Koa.IContext, next) {
     await Convert(_Body())(ctx, next);
 }
開發者ID:wangxin0709,項目名稱:csc-scms-node-koa,代碼行數:3,代碼來源:controller.ts

示例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
//.........這裏部分代碼省略.........
開發者ID:Soluto,項目名稱:simple-fake-server,代碼行數:101,代碼來源:FakeServer.ts


注:本文中的koa-body類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。