-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (48 loc) · 1.19 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
53
54
55
56
57
58
59
60
61
const Koa = require('koa');
const app = new Koa();
const PORT = 4000;
const Router = require('koa-router');
const router = new Router();
const bodyParser = require('koa-parser');
app.use(bodyParser());
router.get('/', (ctx) => {
ctx.body = "123Welcome to Koa application";
});
const posts = [
{
"id": 1,
"name": "Nodejs developer",
"content": "This is about nodejs deverloper"
},
{
"id": 2,
"name": "Viewjs dev",
"content": "Thsi is about view js developement"
},
{
"id": 3,
"name": "Angular dev",
"content": "Thsi is about angular js developement"
}
]
router.get('/posts', (ctx) =>{
ctx.body = post;
});
router.post('/posts', (ctx) =>{
console.log(ctx.request.body);
let {id, name, content} = ctx.request.body;
if(!id){
ctx.throw(400, "Id is required field");
}
if(!name){
ctx.throw(400, "Name is required field");
}
if(!content){
ctx.throw(400, "content is required field");
}
posts.push({id, name, content});
ctx.body = posts
});
app.use(router.routes());
app.listen(PORT);
console.log(`Server is listenning on PORT ${PORT}`);