forked from gSchool/fs-pet-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestfulExpressServer.js
170 lines (148 loc) · 3.78 KB
/
restfulExpressServer.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
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
'use strict'
const fs = require('fs');
const path = require('path');
const petsPath = path.join('./', 'pets.json');
const express = require('express');
const app = express();
const port = process.env.PORT || 8000;
const bodyParser = require('body-parser');
const morgan = require('morgan');
const basicAuth = require('basic-auth');
app.use(bodyParser.json());
app.use(morgan("combined"));
const auth = function (req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm="Required"');
return res.send(401);
};
const user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
};
if (user.name === 'admin' && user.pass === 'meowmix') {
return next();
} else {
return unauthorized(res);
};
};
app.use(auth);
app.post('/pets', (req, res) => {
fs.readFile(petsPath, 'utf8', (err, petsJSON) => {
const petsArray = JSON.parse(petsJSON)
const age = Number(req.body.age);
const kind = req.body.kind;
const name = req.body.name;
const pet = {'age': age, 'kind': kind, 'name': name};
if (!age || !kind || !name) {
console.error(`shit happens`);
return res.sendStatus(400);
}
petsArray.push(pet);
const JSONpets = JSON.stringify(petsArray);
fs.writeFile(petsPath, JSONpets, function(writeErr) {
if(writeErr){
throw writeErr;
}
res.set('Content-Type', 'application/json');
res.send(pet);
});
})
})
app.get('/pets', auth, (req, res) => {
console.log("hello");
fs.readFile(petsPath, 'utf8', (err, petsJSON) => {
if (err) {
console.log(err.stack);
return res.sendStatus(500);
}
const pets = JSON.parse(petsJSON);
res.set('Content-Type', 'application/json');
res.send(pets);
})
})
app.get('/pets/:id', (req, res) => {
fs.readFile(petsPath, 'utf8', (err, petsJSON) => {
if (err) {
console.log(err.stack);
return res.sendStatus(500);
}
const id = Number.parseInt(req.params.id);
const pets = JSON.parse(petsJSON);
const pet = pets[id];
const petString = JSON.stringify(pet);
if (!pet){
res.set('Content-Type', 'text/plain');
res.sendStatus(404);
} else if (id < 0 || id >= pets.length || Number.isNaN(id)) {
res.set('Content-Type', 'text/plain');
res.sendStatus(404);
} else {
res.set('Content-Type', 'application/json');
res.send(petString);
}
});
});
app.patch('/pets/:id', (req, res) => {
fs.readFile(petsPath, 'utf8', (err, petsJSON) => {
if (err) {
console.log(err.stack);
return res.sendStatus(500);
}
const id = Number.parseInt(req.params.id);
const pets = JSON.parse(petsJSON);
const pet = pets[id];
const age = Number(req.body.age);
const kind = req.body.kind;
const name = req.body.name;
if (kind){
pet.kind = kind;
}
if (age){
pet.age = age;
}
if (name){
pet.name = name
}
const petsString = JSON.stringify(pets);
fs.writeFile(petsPath, petsString, function(writeErr) {
if(writeErr){
throw writeErr;
}
res.set('Content-Type', 'application/json');
res.send(pet);
});
})
})
app.delete('/pets/:id', (req, res) => {
fs.readFile(petsPath, 'utf8', (err, petsJSON) => {
if (err) {
console.log(err.stack);
return res.sendStatus(500);
}
const id = Number.parseInt(req.params.id);
const pets = JSON.parse(petsJSON);
console.log(pets)
const pet = pets[id];
pets.splice(id, 1);
const petsString = JSON.stringify(pets);
fs.writeFile(petsPath, petsString, function(writeErr) {
if(writeErr){
throw writeErr;
}
res.set('Content-Type', 'application/json');
res.send(pet);
});
})
})
app.use((req, res, next) => {
res.sendStatus(404);
})
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
app.listen(port, function(){
console.log('Listening on port', port);
})
module.exports = app;