forked from awslabs/amazon-ecs-nodejs-microservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (41 loc) · 1.29 KB
/
server.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const app = require('koa')();
const router = require('koa-router')();
const db = require('./db.json');
// Log requests
app.use(function *(next){
const start = new Date;
yield next;
const ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
router.get('/api/users', function *(next) {
this.body = db.users;
});
router.get('/api/users/:userId', function *(next) {
const id = parseInt(this.params.userId);
this.body = db.users.find((user) => user.id == id);
});
router.get('/api/threads', function *() {
this.body = db.threads;
});
router.get('/api/threads/:threadId', function *() {
const id = parseInt(this.params.threadId);
this.body = db.threads.find((thread) => thread.id == id);
});
router.get('/api/posts/in-thread/:threadId', function *() {
const id = parseInt(this.params.threadId);
this.body = db.posts.filter((post) => post.thread == id);
});
router.get('/api/posts/by-user/:userId', function *() {
const id = parseInt(this.params.userId);
this.body = db.posts.filter((post) => post.user == id);
});
router.get('/api/', function *() {
this.body = "API ready to receive requests";
});
router.get('/', function *() {
this.body = "Ready to receive requests";
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);