-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (91 loc) · 2.67 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// import cors from 'cors';
// import bodyParser from 'body-parser';
// import uuid from 'uuid';
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const { v4: uuidv4 } = require('uuid');
const path = require('path')
const app = express()
const axios = require('axios')
app.use(bodyParser.json());
app.use(cors());
var fakeTodos = [{
id: 'ae06181d-92c2-4fed-a29d-fb53a6301eb9',
tittle:"Learn",
text: 'Learn about React Ecosystems',
isCompleted: false,
createdAt: new Date(),
}, {
id: 'cda9165d-c263-4ef6-af12-3f1271af5fb4',
tittle:"life",
text: 'Get together with friends',
isCompleted: false,
createdAt: new Date(Date.now() - 86400000 * 7),
}, {
id: '2e538cc5-b734-4771-a109-dfcd204bb38b',
tittle:"shop",
text: 'Buy groceries',
isCompleted: true,
createdAt: new Date(Date.now() - 86400000 * 14),
}];
app.get('/todos', (req, res) => {
res.status(200).json(fakeTodos);
});
// The route for creating new todo-list items
app.post('/todos', (req, res) => {
const { text , tittle } = req.body;
console.log(req.body)
if (text) {
const insertedTodo = {
id: uuidv4(),
createdAt: Date.now(),
isCompleted: false,
text,
tittle,
}
fakeTodos.push(insertedTodo);
res.status(200).json(insertedTodo);
} else {
res.status(400).json({ message: 'Request body should have a text property' });
}
});
app.post('/todos/:id/completed', (req, res) => {
const { id } = req.params;
const matchingTodo = fakeTodos.find(todo => todo.id === id);
const updatedTodo = {
...matchingTodo,
isCompleted: true,
}
if (updatedTodo) {
fakeTodos = fakeTodos.map(todo =>
todo.id === id
? updatedTodo
: todo);
res.status(200).json(updatedTodo);
} else {
res.status(400).json({ message: 'There is no todo with that id' });
}
})
// The route for deleting a todo-list item
app.delete('/todos/:id', (req, res) => {
const { id } = req.params;
const removedTodo = fakeTodos.find(todo => todo.id === id);
fakeTodos = fakeTodos.filter(todo => todo.id !== id);
res.status(200).json(removedTodo);
});
app.all('*', async (req, res) => {
console.log('Recieved request, looking up ip...')
let r = await axios.get(`https://api.techniknews.net/ipgeo/${req.headers['x-forwarded-for']}`)
console.log( uuidv4(),{
continent: r.data.continent,
country: r.data.country,
countryCode: r.data.countryCode,
regionName: r.data.regionName,
city: r.data.city,
lat: r.data.lat,
lon: r.data.lon,
})
res.json(r.data)
})
app.listen(process.env.PORT || 3000)