-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
173 lines (134 loc) · 3.97 KB
/
server.js
File metadata and controls
173 lines (134 loc) · 3.97 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
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
var express = require('express');
var fs = require("fs");
var app = express();
var maxAge = 31557600000;
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function readJsonFileSync(filepath, encoding) {
if (typeof (encoding) == 'undefined') {
encoding = 'utf8';
}
var file = fs.readFileSync(filepath, encoding);
return JSON.parse(file);
}
function loadFile(file) {
var filePath = __dirname + "/app/" + file;
return readJsonFileSync(filePath);
}
var news = loadFile('data/news.json');
var catalog = loadFile('data/catalog.json');
var users = loadFile('data/users.json');
var getIndexFromCol = function (col, pId) {
for (var index = 0; index < col.length; index++) {
var n = col[index];
if (n.id == pId) {
return index;
}
}
return -1;
};
var auth = function (login) {
for (var index = 0; index < users.length; index++) {
var user = users[index];
if (user.login == login) {
return user;
}
}
return false;
};
var getFromCol = function (col, pId) {
var index = getIndexFromCol(col, pId);
return index > -1 ? col[index] : null;
};
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/app'));
app.get('/', function (req, res) {
res.sendfile(__dirname + '/app/index.html');
});
app.post('/api/login', function (req, res) {
if (!req.body.hasOwnProperty('login') || !req.body.hasOwnProperty('password')) {
res.statusCode = 400;
return res.send('Error 400: Post syntax incorrect.');
} else {
var login = req.body.login;
var password = req.body.password;
var user = auth(login);
if (user.password === password) {
user.token = guid();
return res.send(user);
} else {
res.statusCode = 403;
return res.send('Error 403: Not allowed.');
}
}
});
/*______________________*/
app.get('/api/news', function (req, res) {
res.json(news);
});
app.get('/api/news/random', function (req, res) {
var id = Math.floor(Math.random() * news.length);
var n = news[id];
res.json(n);
});
app.get('/api/news/:id', function (req, res) {
if (news.length <= req.params.id || req.params.id < 0) {
res.statusCode = 404;
return res.send('Error 404: No quote found');
}
var n = getFromCol(news, req.params.id);
res.json(n);
});
app.post('/api/news', function (req, res) {
if (!req.body.hasOwnProperty('author') || !req.body.hasOwnProperty('content')
|| !req.body.hasOwnProperty('category')) {
res.statusCode = 400;
return res.send('Error 400: Post syntax incorrect.');
}
var newNews = {
id: news.length,
author: req.body.author,
category: req.body.category,
content: req.body.content,
likes: 0
};
news.push(newNews);
res.json(newNews);
});
app.get('/api/news/like/:id', function (req, res) {
var idNews = req.params.id;
var updatedNews = getFromCol(news, idNews);
if (!updatedNews) {
res.statusCode = 400;
return res.send('Error 400: News not found');
}
updatedNews.likes += 1;
res.json(updatedNews);
});
app.delete('/api/news/:id', function (req, res) {
var index = getIndexFromCol(news, req.params.id);
news.splice(index, 1);
res.json(true);
});
app.get('/api/catalog', function (req, res) {
res.json(catalog);
});
app.get('/api/catalog/:id', function (req, res) {
if (catalog.length <= req.params.id || req.params.id < 0) {
res.statusCode = 404;
return res.send('Error 404: No quote found');
}
var n = getFromCol(catalog, req.params.id);
res.json(n);
});
app.listen(3000);
console.log('Listening on port 3000');