-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (25 loc) · 834 Bytes
/
index.js
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
import Koa from 'koa';
import mongoose from 'mongoose';
mongoose.connect('mongodb://db:27017/test');
const Book = mongoose.model('Book', {name: String, author: String});
const app = new Koa();
// response
app.use(async (ctx) => {
if (ctx.request.url.includes('/create')) {
const osef = new Book({name: String(Math.random()), author: 'qsd2'});
await osef.save();
ctx.body = 'Created';
} else if (ctx.request.url.includes('/read')) {
ctx.body = JSON.stringify(await Book.find().exec());
} else if (ctx.request.url.includes('/update')) {
const x = (await Book.find().exec())[0];
x.name = 'bob';
await x.save();
ctx.body = 'OK';
} else if (ctx.request.url.includes('/delete')) {
const x = (await Book.find().exec())[0];
await x.delete();
ctx.body = 'OK';
}
});
app.listen(3000);