repl.it
@Vandesm14/

Blog Recode

Express

No description

fork
loading
Files
  • index.js
  • pages
  • posts
  • public
  • db.json
  • template.html
  • Packager files
  • package-lock.json
  • package.json
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');

const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

const db = {
	queryRecord: (name) => {
		return queryRecord(configPosts, 'name', name);
	},
	updatePosts: () => {
		let ret = [];
		let fileNames = fs.readdirSync(__dirname + '/posts');
		let dbObj = JSON.parse(fs.readFileSync(__dirname + '/db.json', 'utf8'));
		let dbNames = getFields(dbObj, 'name');

		for (let i in fileNames) {
			if (dbNames.indexOf(fileNames[i]) !== -1) {
				ret.push(dbObj[dbNames.indexOf(fileNames[i])]);
			} else {
				ret.push({ name: fileNames[i], date: new Date().toISOString()});
			}
		}

		configPosts = copy(ret);
		db.writePosts();
		console.log(configPosts);
	},
	writePosts: () => {
		fs.writeFileSync(__dirname + '/db.json', JSON.stringify(configPosts), 'utf8');
	}
};

var allPosts = [];
var configPosts = [];
var posts = [];

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use('/post', express.static('public'));

http.listen(3000, function () {
	console.log('listening on *:3000');
	startup();
});

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/public/index.html');
});

app.get('/post/*', (req, res) => {
	res.sendFile(__dirname + '/public/index.html')
});

app.get('/all/:level', (req, res) => {
	res.send(posts.slice(0, req.params.level * 10));
});

app.get('/getPost/:id', (req, res) => {
	res.send(getSinglePost(req.params.id + '.md'));
});

function startup() {
	db.updatePosts();
	console.log(configPosts);
	getAllPosts();
	sortAllPosts();
}

function getAllPosts() {
	let files = fs.readdirSync(__dirname + '/posts');
	allPosts = [];

	for (let i in files) {
		let obj = {};
		obj.title = toTitleCase(files[i].substr(0, files[i].length - 3).replace(/\-/g, ' '));
		obj.body = fs.readFileSync(__dirname + '/posts/' + files[i], 'utf8').replace('\n', '<br>');
		obj.date = db.queryRecord(files[i]).date;
		allPosts.push(obj);
	}
}

function sortAllPosts() {
	let newPosts = copy(allPosts);
	newPosts.sort((a, b) => (new Date(a.date) < new Date(b.date)) ? 1 : -1);
	posts = copy(newPosts);
}

function getSinglePost(name) {
	let obj = {};
	obj.title = toTitleCase(name.substr(0, name.length - 3).replace(/\-/g, ' '));
	obj.body = fs.readFileSync(__dirname + '/posts/' + name, 'utf8');
	obj.date = db.queryRecord(name).date;
	return obj;
}

fs.watch(__dirname + '/posts', (eventType, filename) => {
	let files = fs.readdirSync(__dirname + '/posts');
	db.updatePosts();
	getAllPosts();
	sortAllPosts();
	io.emit('new', '');
});

function toTitleCase(str) {
	return str.replace(/\w\S*/g, function (txt) {
		return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
	});
}

function copy(obj) {
	return JSON.parse(JSON.stringify(obj));
}

function parseENV() {
	let ret = {};
	fs.readFileSync('.env', 'utf8').split('\n').forEach((e) => {
		ret[e.split('=')[0]] = e.split('=')[1];
	});
	return ret;
}

function getFields(obj, field) {
	let ret = [];
	for (let i in obj) {
		ret.push(obj[i][field]);
	}
	return ret;
}

function queryRecord(obj, field, query) {
	let ret;
	for (let i in obj) {
		if (obj[i][field] === query) {
			return obj[i];
		}
	}
}
?