@Vandesm14/

Blog Recode

Express

No description

fork
loading
Files
  • index.js
  • pages
  • posts
  • public
  • db.json
  • package-lock.json
  • package.json
  • template.html

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.13564801262443593","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgZXhwcmVzcyA9IHJlcXVpcmUoJ2V4cHJlc3MnKTsKY29uc3QgYm9keVBhcnNlciA9IHJlcXVpcmUoJ2JvZHktcGFyc2VyJyk7CmNvbnN0IGZzID0gcmVxdWlyZSgnZnMnKTsKCmNvbnN0IGFwcCA9IGV4cHJlc3MoKTsKY29uc3QgaHR0cCA9IHJlcXVpcmUoJ2h0dHAnKS5TZXJ2ZXIoYXBwKTsKY29uc3QgaW8gPSByZXF1aXJlKCdzb2NrZXQuaW8nKShodHRwKTsKCmNvbnN0IGRiID0gewoJcXVlcnlSZWNvcmQ6IChuYW1lKSA9PiB7CgkJcmV0dXJuIHF1ZXJ5UmVjb3JkKGNvbmZpZ1Bvc3RzLCAnbmFtZScsIG5hbWUpOwoJfSwKCXVwZGF0ZVBvc3RzOiAoKSA9PiB7CgkJbGV0IHJldCA9IFtdOwoJCWxldCBmaWxlTmFtZXMgPSBmcy5yZWFkZGlyU3luYyhfX2Rpcm5hbWUgKyAnL3Bvc3RzJyk7CgkJbGV0IGRiT2JqID0gSlNPTi5wYXJzZShmcy5yZWFkRmlsZVN5bmMoX19kaXJuYW1lICsgJy9kYi5qc29uJywgJ3V0ZjgnKSk7CgkJbGV0IGRiTmFtZXMgPSBnZXRGaWVsZHMoZGJPYmosICduYW1lJyk7CgoJCWZvciAobGV0IGkgaW4gZmlsZU5hbWVzKSB7CgkJCWlmIChkYk5hbWVzLmluZGV4T2YoZmlsZU5hbWVzW2ldKSAhPT0gLTEpIHsKCQkJCXJldC5wdXNoKGRiT2JqW2RiTmFtZXMuaW5kZXhPZihmaWxlTmFtZXNbaV0pXSk7CgkJCX0gZWxzZSB7CgkJCQ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pZWxkKSB7CglsZXQgcmV0ID0gW107Cglmb3IgKGxldCBpIGluIG9iaikgewoJCXJldC5wdXNoKG9ialtpXVtmaWVsZF0pOwoJfQoJcmV0dXJuIHJldDsKfQoKZnVuY3Rpb24gcXVlcnlSZWNvcmQob2JqLCBmaWVsZCwgcXVlcnkpIHsKCWxldCByZXQ7Cglmb3IgKGxldCBpIGluIG9iaikgewoJCWlmIChvYmpbaV1bZmllbGRdID09PSBxdWVyeSkgewoJCQlyZXR1cm4gb2JqW2ldOwoJCX0KCX0KfQ=="},"asBuffer":null},"loaded":true}}
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];
		}
	}
}