-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (114 loc) · 3.26 KB
/
server.js
File metadata and controls
133 lines (114 loc) · 3.26 KB
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
const logger = require('./lib/logger');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 8000;
let lodgings = require('./lodgings');
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(logger);
app.get('/lodgings', function (req, res) {
let page = parseInt(req.query.page) || 1;
const numPerPage = 10;
const lastPage = Math.ceil(lodgings.length / numPerPage);
page = page < 1 ? 1 : page;
page = page > lastPage ? lastPage : page;
const start = (page - 1) * numPerPage;
const end = start + numPerPage;
const pageLodgings = lodgings.slice(start, end);
let links = {};
if (page < lastPage) {
links.nextPage = '/lodgings?page=' + (page + 1);
links.lastPage = '/lodgings?page=' + lastPage;
}
if (page > 1) {
links.prevPage = '/lodgings?page=' + (page - 1);
links.firstPage = '/lodgings?page=1';
}
res.status(200).json({
lodgings: pageLodgings,
pageNumber: page,
totalPages: lastPage,
pageSize: numPerPage,
totalCount: lodgings.length,
links: links
});
});
app.post('/lodgings', function (req, res, next) {
if (req.body && req.body.name && req.body.price && req.body.ownerID) {
const id = lodgings.length;
lodgings.push({
id: id,
name: req.body.name,
price: req.body.price,
ownerID: req.body.ownerID,
description: req.body.description || "No description"
});
res.status(201).json({
id: id,
links: {
lodging: '/lodgings/' + id
}
});
} else {
res.status(400).json({
err: "Request needs a JSON body with a name, a price, and an owner ID"
});
}
});
app.get('/lodgings/:lodgingID', function (req, res, next) {
const lodgingID = parseInt(req.params.lodgingID);
if (lodgings[lodgingID]) {
res.status(200).json(lodgings[lodgingID]);
} else {
next();
}
});
app.put('/lodgings/:lodgingID', function (req, res, next) {
const lodgingID = parseInt(req.params.lodgingID);
if (lodgings[lodgingID]) {
if (req.body && req.body.name && req.body.price && req.body.ownerID) {
lodgings[lodgingID] = {
name: req.body.name,
price: req.body.price,
ownerID: req.body.ownerID,
description: req.body.description || "No description"
};
res.status(200).json({
links: {
lodging: '/lodgings/' + lodgingID
}
});
} else {
res.status(400).json({
err: "Request needs a JSON body with a name, a price, and an owner ID"
});
}
} else {
next();
}
});
app.delete('/lodgings/:lodgingID', function (req, res, next) {
const lodgingID = parseInt(req.params.lodgingID);
if (lodgings[lodgingID]) {
lodgings[lodgingID] = null;
res.status(204).end();
} else {
next();
}
});
app.get('/users/:userID/lodgings', function (req, res, next) {
const ownerID = parseInt(req.params.userID);
const ownerLodgings = lodgings.filter(lodging => lodging.ownerID === ownerID);
res.status(200).json({
lodgings: ownerLodgings
});
});
app.use('*', function (req, res, next) {
res.status(404).json({
err: "Path " + req.url + " does not exist"
});
});
app.listen(port, function() {
console.log("== Server is running on port", port);
});