-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (40 loc) · 1.23 KB
/
index.js
File metadata and controls
47 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const Koa = require("koa");
const koaBody = require("koa-body");
const writeGood = require("write-good");
const app = (module.exports = new Koa());
const port = process.env.PORT || 3000;
app.use(
koaBody({
jsonLimit: "1kb"
})
);
// co-body accepts application/json
// and application/x-www-form-urlencoded
app.use(async function(ctx) {
const body = ctx.request.body;
if (!body.text) ctx.throw(400, '.text required');
if(!body.options) ctx.body = writeGood(body.text);
else {
if(typeof(body.options) !== "object") ctx.throw(400, '.options should be an object');
ctx.body = writeGood(body.text,body.options);
}
});
// app.use(
// _.get("/", async ctx => {
// ctx.body =
// "Send a POST request to /api containing `text` (string) and, optionally, `options` (object).";
// })
// );
// app.use(
// _.get("/api", async ctx => {
// const body = ctx.request.body;
// if (!body.text) ctx.throw(400, ".text required");
// if (!body.options) ctx.body = writeGood(body.text);
// else {
// if (typeof body.options !== "object")
// ctx.throw(400, ".options should be an object");
// ctx.body = writeGood(body.text, body.options);
// }
// })
// );
if (!module.parent) app.listen(port);