This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
201 lines (176 loc) · 5.33 KB
/
server.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser'); // Parser for Express
const methodOverride = require('method-override'); // Add PUT and DELETE supprot
const morgan = require('morgan'); // Logger for Express
const log = require('./libs/log')(module); // Logger
const app = express();
app.use(morgan('dev')); // Log all reqs into console
app.use(bodyParser.json()); // JSON Parser
app.use(express.urlencoded())
app.use(methodOverride()); // поддержка put и delete
// app.use(express.static(path.join(__dirname, "public"))); // запуск статического файлового сервера, который смотрит на папку public/ (в нашем случае отдает index.html)
app.get('/ErrorExample', function (req, res, next) {
next(new Error('Not valid name'));
});
app.get('/', function (req, res) {
res.send('API is running');
});
app.listen(process.env.PORT || 1337, function(){
log.info('Express server listening on port 1337');
});
const db = require("./libs/database.js");
app.get('/users', async (req, res, next) => {
try {
qres = await db.User.get();
res.send(qres);
} catch (err) {
next(err)
return;
}
});
app.post('/users', async (req, res, next) => {
const user = new db.User(req.body);
console.log(req.body);
user.struct.hashed_password = db.User.getHash(req.body.password)
try {
const newUsr = await user.submit();
res.send(newUsr);
} catch (e) {
next(e);
}
});
app.get('/users/:id', async (req, res, next) => {
if (req.query)
req.body = req.query;
try {
console.log(req.body);
if (req.body.password)
qres = await db.User.get(req.params.id, req.body.password);
else
qres = await db.User.get(req.params.id);
} catch (err) {
next(err)
return;
}
res.send(qres);
});
app.delete('/users/:id', function (req, res) {
if (db.User.delete(req.params.id, (req.body.password || req.query.password))) {
res.send({result: "User Deleted Succesfully"});
} else {
res.send({result: "Error while deleting"});
}
});
app.get('/users/:uid/devices', async (req, res, next) => {
if (req.query)
req.body = req.query;
try {
qres = await db.Device.get(req.params.uid);
res.send(qres);
} catch (err) {
next(err)
return;
}
});
app.get('/users/:uid/devices/:did', async (req, res, next) => {
try {
qres = await db.Device.get(req.params.uid, req.params.did);
res.send(qres);
} catch (err) {
next(err);
return;
}
});
app.post('/users/:uid/devices', async (req, res, next) => {
const obj = req.body;
obj.owner_id = req.params.uid;
const device = new db.Device(obj);
try {
const newDevice = await device.submit();
res.send(newDevice);
} catch (err) {
next(err);
return;
}
})
app.put('/users/:uid/devices/:did/', async (req, res, next) => {
const obj = req.body;
obj.id = req.params.did;
const device = new db.Device(obj);
try {
const changedDevice = await device.submit();
res.send(changedDevice);
} catch (e) {
next(e);
return;
}
});
app.delete('/users/:uid/devices/:did', async (req, res, next) => {
if (req.query)
req.body = req.query;
if (await db.Device.delete(req.params.uid, req.params.did, (req.body.password || req.query.password))) {
res.send({ result: "Device Deleted Succesfully" });
} else {
res.send({ result: "Error while deleting" });
}
});
app.get('/users/:uid/devices/:did/props', async (req, res, next) => {
try {
qres = await db.DeviceProp.get(req.params.did);
res.send(qres);
} catch (err) {
next(err);
return;
}
});
app.post('/users/:uid/devices/:did/props', async (req, res, next) => {
const obj = req.body;
obj.device_id = req.params.did;
const deviceProp = new db.DeviceProp(obj);
try {
const newDeviceProp = await deviceProp.submit();
res.send(newDeviceProp);
} catch (err) {
next(err);
return;
}
});
app.put('/users/:uid/devices/:did/props/:pid', async (req, res, next) => {
const obj = req.body;
obj.device_id = req.params.did;
const deviceProp = new db.DeviceProp(obj);
try {
const changedDeviceProp = await deviceProp.submit();
res.send(changedDeviceProp);
} catch (e) {
next(e);
return;
}
});
app.delete('/users/:uid/devices/:did/props/:pid', async (req, res, next) => {
if (req.query)
req.body = req.query;
try {
await db.DeviceProp.delete(req.params.uid, req.params.did, req.params.pid, (req.body.password || req.query.password))
res.send({ result: "Device Property deleted succesfully" });
} catch (err) {
next(err);
return;
}
});
// 404 Handler
app.use(function (req, res, next) {
res.status(404);
log.debug('Not found URL: %s', req.url);
res.send({ error: 'Not found' });
return;
});
// 500 Handler
app.use(function (err, req, res, next) {
res.status(err.status || 500);
log.error(`Internal error(${res.statusCode}): ${err.message}`);
console.log(err);
res.send({ error: err.message });
return;
});