-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
96 lines (83 loc) · 2.59 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
var express = require('express');
var low = require('lowdb');
var FileSync = require('lowdb/adapters/FileSync');
var adapter = new FileSync('db.json'); // More details in README.md
var db = low(adapter);
var app = express();
var creds = require("./secret.json"); // More details in README.md
db.defaults({
posts: [],
meta: {}
}).write();
app.use(express.static('public'));
app.get("/", function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get("/admin", function (req, res) {
res.sendFile(__dirname + '/public/admin.html');
});
app.get("/api/v1", function (req, res) {
res.sendFile(__dirname + '/db.json');
});
app.get("/api/v1/posts", function (req, res) {
res.send(db.get('posts'));
});
app.get("/api/v1/posts/:ID", function (req, res) {
res.send(db.get('posts.' + req.params.ID));
});
app.get("/api/v1/meta/:ID", function (req, res) {
res.send(db.get('meta.' + req.params.ID));
});
app.get("/api/v1/meta", function (req, res) {
res.send(db.get('meta'));
});
app.post("/posts/new", function (req, res) {
if (req.query.title && req.query.date && req.query.body && req.query.token == creds.secret) {
db.get('posts')
.push({
id: db.get('posts').size() + 1, // More deatils in README.md
title: req.query.title,
date: req.query.date,
content: req.query.body
})
.write();
res.send(db.get('posts').last());
} else {
res.send(JSON.stringify({
"error": "missing parameter"
}));
}
});
app.post("/meta/:ID", function (req, res) {
var ID = req.params.ID;
if (ID == "link" && req.query.text && req.query.value && req.query.token == creds.secret) {
var linkTxt = req.query.text; // More deatils in README.md
var linkVal = req.query.value;
db.get('meta.link')
.assign({
title: linkTxt,
value: linkVal
})
.write();
res.send(JSON.stringify({
link: db.get('meta.link').value()
}));
}
if (req.query.value && req.query.token == creds.secret) {
db.get('meta')
.assign({
[ID]: req.query.value
})
.write();
res.send(JSON.stringify({
[ID]: db.get('meta.' + ID).value()
}));
} else {
res.send(JSON.stringify({
"error": "missing parameter"
}));
}
});
var listener = app.listen(3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});