-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (158 loc) · 4.44 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Controller - Express is cool
let express = require('express');
let app = express();
let petRepo = require('./repos/petRepo');
let errorHelper = require('./helpers/errorHelpers');
let cors = require('cors');
// Express Router object
let router = express.Router();
// Middleware support for JSON parsing in request object
app.use(express.json());
// Configure CORS
app.use(cors());
// GET return all
router.get('/', function (req, res, next) {
petRepo.get(function (data) {
res.status(200).json({
"status": 200,
"statusText": "OK",
"message": "All pets retrieved.",
"data": data
});
}, function(err) {
next(err);
});
});
// Create GET/search?id=n&name=str to search for pies by 'id' and/or 'name'
router.get('/search', function (req, res, next) {
let searchObject = {
"id": req.query.id,
"type": req.query.type,
"breed": req.query.breed,
"size": req.query.size,
"age": req.query.age,
"gender": req.query.gender,
"color": req.query.color,
"name": req.query.name
};
petRepo.search(searchObject, function (data) {
res.status(200).json({
"status": 200,
"statusText": "OK",
"message": "All searched pets retrieved.",
"data": data
});
}, function (err) {
next(err);
});
});
// Get by ID
router.get('/:id', function (req, res, next) {
petRepo.getById(req.params.id, function (data) {
if (data) {
res.status(200).json({
"status": 200,
"statusText": "OK",
"message": "Single pet retrieved.",
"isKeithCool": true,
"data": data
});
}
else {
res.status(404).json({
"status": 404,
"statusText": "Not Found",
"message": "Pet ID: " + req.params.id + " could not be found.",
"isKeithCool": true,
"error": {
"code": "NOT_FOUND",
"message": "Pet ID: " + req.params.id + " could not be found."
}
});
}
}, function(err) {
next(err);
});
});
// router.post('/', function (req, res, next) {
// petRepo.insert(req.body, function (data) {
// res.status(201).json({
// "status": 201,
// "statusText": "Created",
// "message": "New Pet Added.",
// "data": data
// });
// }, function (err) {
// next(err);
// });
// });
// router.put('/:id', function (req, res, next) {
// petRepo.getById(req.params.id, function (data) {
// if (data) {
// // Attempt to update the data
// petRepo.update(req.body, req.params.id, function (data) {
// res.status(200).json({
// "status": 200,
// "statusText": "OK",
// "message": "Pet '" + req.params.id + "' updated.",
// "data": data
// });
// });
// }
// else {
// res.status(404).json({
// "status": 404,
// "statusText": "Not Found",
// "message": "Pet '" + req.params.id + "' could not be found.",
// "error": {
// "code": "NOT_FOUND",
// "message": "Pet '" + req.params.id + "' could not be found."
// }
// });
// }
// }, function(err) {
// next(err);
// });
// });
// router.delete('/:id', function (req, res, next) {
// petRepo.getById(req.params.id, function (data) {
// if (data) {
// // Attempt to delete data
// petRepo.delete(req.params.id, function (data) {
// res.status(200).json({
// "status": 200,
// "statusText": "OK",
// "message": "Pet '" + req.params.id + "' is deleted.",
// "data": data
// })
// })
// }
// else {
// res.status(404).json({
// "status": 404,
// "statusText": "Not Found",
// "message": "Pet '" + req.params.id + "' could not be found.",
// "error": {
// "code": "NOT_FOUND",
// "message": "Pet '" + req.params.id + "' could not be found."
// }
// });
// }
// }, function(err) {
// next(err);
// });
// });
// router prefixes all with /aip/v1
app.use('/api/', router);
// Configure exception logger to console
app.use(errorHelper.logErrorsToConsole);
// Configure exception logger to file
app.use(errorHelper.logErrorsToFile);
// Configure client error handler
app.use(errorHelper.clientErrorHandler);
// Configure catch-all exception middleware last
app.use(errorHelper.errorHandler);
// Listen on port 5000
var server = app.listen(5000, function () {
console.log('Node server is running on http://localhost:5000');
});