-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
160 lines (114 loc) · 3.47 KB
/
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const PORT = 8000;
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const fetch = require('node-fetch');
require('dotenv').config();
const app = express();
app.use(morgan('tiny'));
app.use(cors());
app.use(express.json());
app.get('/restaurants', async (req, res) => { // get all restaurants data
const url = process.env.RESTAURANTLIST;
const options = {
method: 'GET',
headers: {
'Accept': 'application/json',
'X-Cassandra-Token': process.env.X_Cassandra_Token
}
}
await fetch(url, options)
.then(response => response.json())
.then(json => res.json(json))
.catch(err => console.log(err))
}
)
app.delete('/deleteRestaurant/:id', async (req, res) => { // delete restaurant data with document id
const url = process.env.DELETERESTAURANT + req.params.id;
const options = {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'X-Cassandra-Token': process.env.X_Cassandra_Token
}
}
await fetch(url, options)
.then(response => {
if (response.status === 204) {
res.status(204).send()
} else {
res.status(500).send()
}
}).catch(err => console.log(err))
}
)
app.post('/addRestaurant', async (req, res) => { // add restaurant to collection
let newRestaurant = req.body;
const url = process.env.ADDRESTAURANT
const options = {
method: 'POST',
headers: {
'Accept': 'application/json',
'X-Cassandra-Token': process.env.X_Cassandra_Token,
'Content-Type': 'application/json'
},
body: JSON.stringify(newRestaurant)
}
await fetch(url, options)
.then(response => response.json())
.then(json => res.json(json))
.catch(err => console.log(err))
}
)
app.get('/restaurant/:id', async (req, res) => { // get a restaurant data with document id
const url = process.env.GETRESTAURANT + req.params.id;
console.log(url)
const options = {
method: 'GET',
headers: {
'Accept': 'application/json',
'X-Cassandra-Token': process.env.X_Cassandra_Token
}
}
await fetch(url, options)
.then(response => response.json())
.then(json => res.json(json))
.catch(err => console.log(err))
}
)
app.put('/editRestaurant/:id', async (req, res) => { // edit restaurant data with document id
const url = process.env.PUTRESTAURANT + req.params.id;
const updatedRestaurant = req.body;
const options = {
method: 'PUT',
headers: {
'Accept': 'application/json',
'X-Cassandra-Token': process.env.X_Cassandra_Token,
'Content-Type': 'application/json'
},
body: JSON.stringify(updatedRestaurant)
}
await fetch(url, options)
.then(response => response.json())
.then(json => res.json(json))
.catch(err => console.log(err))
}
)
function notFound(req, res, next) {
res.status(404);
const error = new Error('Not Found - ');
next(error);
}
function errorHandler(err, req, res) {
res.status(res.statusCode || 500);
res.json({
message: err.message,
stack: err.stack
});
}
app.use(notFound);
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
}
)