-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 1.08 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
const express = require('express');
const Datastore = require('nedb');
const app = express();
app.listen(8080, () => console.log('listening at 8080'));
app.listen(3000, () => console.log('listening at 3000'));
app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
const database = new Datastore('database.db');
database.loadDatabase();
//app.get('/api', (request, response) => {
// database.find({}, (err, data) => {
//if (err) {
// response.end();
//return;
//}
// response.json(data);
//});
//});
app.get('/api', (request, response) => {
database.find({}, (err, data) => {
if (err) {
response.end();
return;
}
response.json(data);
});
});
//app.post('/api', (request, response) => {
//const data = request.body;
//const timestamp = Date.now();
//data.timestamp = timestamp;
//database.insert(data);
//response.json(data);
//});
app.post('/api', (request, response) => {
const data = request.body;
const timestamp = Date.now();
data.timestamp = timestamp;
database.insert(data);
response.json(data);
});