-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (98 loc) · 2.91 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
// server.js
// where your node app starts
// init project
const express = require('express');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('.data/db.json');
const db = low(adapter);
const shortid = require('shortid');
var bodyParser = require('body-parser');
const fs = require('fs');
var showdown = require('showdown');
const converter = new showdown.Converter();
var app = express();
app.use( bodyParser.json() );
db.defaults({ posts: [] })
.write()
app.get("/", function (equest, response) {
var path = __dirname + '/README.md';
var file = fs.readFileSync(path, 'utf8');
response.status(200).send(converter.makeHtml(file.toString()));
});
// this is probably a bad idea
// app.get("/reset", function (request, response ) {
// // removes all entries from the collection
// db.get('posts')
// .remove()
// .write();
// console.log("Database cleared");
// response.status(200).send("Database cleared");
// });
app.get("/ids", function(request, response) {
var ids = db
.get('posts')
.map('id')
.value();
if(ids) {
response.status(200).send('<pre>' + JSON.stringify(ids, null, '\t') + '</pre>');
} else {
response.sendStatus(404);
}
});
app.get("/last", function(request, response) {
var post = db
.get('posts')
.last()
.value();
if(post) {
response.status(200).send('<pre>' + JSON.stringify(post, null, '\t') + '</pre>');
} else {
response.sendStatus(404);
}
})
app.get("/:postId", function(request, response) {
console.log(request.params.postId);
var post = db
.get('posts')
.find({id:request.params.postId})
.value();
if(post) {
response.status(200).send('<pre>' + JSON.stringify(post, null, '\t') + '</pre>');
} else {
response.sendStatus(404);
}
})
app.get("/:postId/body", function(request, response) {
console.log(request.params.postId);
var post = db
.get('posts')
.find({id:request.params.postId})
.value();
if(post) {
response.status(200).send('<pre>' + JSON.stringify(post.body, null, '\t') + '</pre>');
} else {
response.sendStatus(404);
}
})
app.post("/", function (request, response) {
console.log(request.body);
<<<<<<< HEAD
console.log(request);
db.get('posts')
.push({id:shortid.generate(),timestamp:(new Date()).toJSON(),ips:request.get('x-forwarded-for'),body:request.body, headers:request.headers})
.write();
// TODO: notify that there's a new POST
response.status(201).send();
=======
db.get('posts')
.push({id:shortid.generate(),timestamp:(new Date()).toJSON(),ips:request.get('x-forwarded-for'),body:request.body})
.write();
// TODO: notify that there's a new POST
response.status(201).send();
>>>>>>> 48e02b394ec517ae1818f2bcba1a9a17e036690d
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});