-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (78 loc) · 2.74 KB
/
Copy pathindex.js
File metadata and controls
115 lines (78 loc) · 2.74 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
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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const ip = '10.0.1.77';
const port = 80;
app.set('view engine', 'ejs');
app.use(express.json({ limit: '50mb' }));
app.use(express.static('public/'));
app.get('/', (req, res) => {
let json = fs.readFileSync('./posts.json', 'utf-8', (err, data) => {
});
json = JSON.parse(json);
res.render('index', { posts: json.posts, comment: false });
})
app.post('/images/:image', (req, res) => {
res.sendFile(path.join(__dirname, `images/${req.params.image}`));
})
app.post('/submit-post', (req, res) => {
let json = fs.readFileSync('./posts.json', 'utf-8', (err, data) => {
});
json = JSON.parse(json);
const author = req.body.author;
const content = req.body.content;
const imageName = req.body.imageName;
const id = Date.now();
const base64Data = req.body.image;
let binaryData
if (base64Data) binaryData = Buffer.from(base64Data, "base64");
const date = new Date();
let formattedDate = date.getFullYear() + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + date.getDate()).slice(-2) + ' ' +
('0' + date.getHours()).slice(-2) + ':' +
('0' + date.getMinutes()).slice(-2) + ':' +
('0' + date.getSeconds()).slice(-2);
if (req.body.postID != undefined) {
json.posts.forEach(post => {
if (post.id === req.body.postID) {
post.comments.unshift({
"author": author,
"timestamp": formattedDate,
"image": imageName,
"content": content,
});
}
});
}
else {
json.posts.unshift({
"id": id,
"author": author,
"timestamp": formattedDate,
"image": imageName,
"content": content,
"comments": [],
});
}
json = JSON.stringify(json);
fs.writeFileSync('./posts.json', json);
if (binaryData) fs.writeFileSync(`./images/${imageName}`, binaryData);
res.send('Posted');
})
app.get('/post/:id', (req, res) => {
let json = fs.readFileSync('./posts.json', 'utf-8');
json = JSON.parse(json);
let post = new Array(1);
for (let i = 0; i < json.posts.length; i++) {
if (json.posts[i].id === parseInt(req.params.id)) {
post[0] = json.posts[i]
}
}
res.render('index', { posts: post, comment: true });
})
app.all('*', (req, res) => {
res.status(404).render('404');
});
app.listen(port, ip, () => { console.log(`serving on: ${ip}:${port}`) });