-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (51 loc) · 1.38 KB
/
index.js
File metadata and controls
59 lines (51 loc) · 1.38 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
// b14 (buddyboard v2)
// by repeat-tech for @friends2013
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const YAML = require('yaml');
const bodyParser = require('body-parser');
const uuid = require('uuid');
app.use(bodyParser.urlencoded({
extended: true
}));
// views
app.use('/static', express.static('./src/static'))
app.set('views', './src/views');
app.set('view engine', 'pug');
// functions
let postCount = 1;
function loadPosts() {
const posts = fs.readFileSync('./posts.yml', 'utf8')
return YAML.parse(posts);
};
// routes
app.get('/', (request, response) => {
const posts = loadPosts()
response.render('index', {"posts" : posts, "id" : uuid.v1()});
});
app.post('/', (request, response) => {
app.use(bodyParser.json());
const content = request.body.content;
const newPost = {
id: postCount++,
author: uuid.v1(),
ip: request.socket.remoteAddress,
content,
};
let posts = loadPosts();
if (!Array.isArray(posts)) {
posts = [];
}
posts.push(newPost);
fs.writeFileSync('posts.yml', YAML.stringify(posts, { indent: 2 }), 'utf8');
response.redirect('/');
});
app.get('/api', (request, response) => {
const posts = loadPosts();
response.send(posts);
});
app.listen(5000, () => {
console.log('App is listening on port 5000');
});