-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (44 loc) · 1.24 KB
/
app.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
import express from 'express';
import db from './db/db';
import bodyParser from 'body-parser';
// Set up the express app
const app = express();
// Parse incoming requests data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/api/v1/todos', (req, res) => {
console.log(req.body);
if(!req.body.title) {
return res.status(400).send({
success : 'false',
message : 'title is required'
});
} else if(!req.body.description) {
return res.status(400).send({
success : 'false',
message : 'description is required'
});
}
const todo = {
id : db.length + 1,
title : req.body.title,
description : req.body.description
}
db.push(todo);
return res.status(201).send({
success : 'true',
message : 'todo added successfully'
})
});
// get all todos
app.get('/api/todos', (req, res) => {
res.status(200).send({
success : 'true',
message : 'you have got the data successfully',
todos : db
})
});
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Server is running on port - ${PORT}`)
});