-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
283 lines (252 loc) · 8.65 KB
/
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const express = require('express');
const fs = require("fs");
const {
render
} = require("mustache");
const sitedata = require("./config.json");
const slugify = require("slugify");
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const {
check,
validationResult
} = require('express-validator');
const basicAuth = require('express-basic-auth');
const app = express();
const port = 3005;
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.set('view engine', 'hbs');
app.use(express.static('public'));
app.set('views', './views');
app.use(cookieParser());
const csrfProtection = csrf({
cookie: true
});
app.listen(port, function() {
console.log('listening on port ' + port);
});
function getCurrentDate(n) {
return (n < 10 ? "0" : "") + n
}
const date = new Date()
const month = getCurrentDate(date.getMonth() + 1)
const day = getCurrentDate(date.getDate())
const year = date.getFullYear()
const formattedDate = year + "-" + month + "-" + day
app.get('/', csrfProtection, function(req, res) {
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
res.render('home', {
post: {
title: 'Create New Markdown Post',
description: 'Create New Markdown Blog Post.',
csrfToken: req.csrfToken()
}
});
});
app.get('/markdown', function(req, res) {
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
res.render('editor', {
post: {
title: 'Create New Markdown Post',
description: 'Create New Markdown Blog Post.'
}
});
});
app.post('/data', [
check('title', 'title length should be 50 to 60 characters Good for SEO')
.isLength({
min: 10,
max: 65
}),
check('description', 'description length should be 100 to 140 characters Good for SEO')
.isLength({
min: 60,
max: 155
}),
check('postcontent', 'Fill Some Post Content').not().isEmpty().trim().escape(),
check('tag', 'Enter Atleast one Tag for Post').not().isEmpty().trim().escape(),
], function(req, res) {
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
const blog_title = req.body.title
const random_id = Math.floor(1000 + Math.random() * 9000)
const basename = sitedata.url_data + "-" + random_id
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(400).json(errors);
} else {
const seo_url = slugify(blog_title, {
replacement: '-',
remove: /[*+~.()'"!:@]/g,
lower: true,
strict: false
});
var title = blog_title;
var description = req.body.description;
var date = formattedDate;
var tag = req.body.tag;
var postcontent = req.body.postcontent;
let content = [{
title: title || "Example Post title",
description: description || "Example Post description",
date: date,
tag: tag || "Hello World",
postcontent: postcontent || "Example Post Content",
slug: decodeURIComponent(seo_url)
}];
let template = fs.readFileSync("./template.md").toString()
content.forEach(post_data => {
let output = render(template, post_data)
const clean_url = basename;
fs.writeFileSync(`${sitedata.storage_path}/${clean_url}.${sitedata.format}`, output)
console.log(post_data);
})
res.status(200).json({
sucess: 1,
message: 'Post Created'
});
}
});
app.post('/post', csrfProtection, [
check('title', 'title length should be 50 to 60 characters Good for SEO')
.isLength({
min: 10,
max: 65
}),
check('description', 'description length should be 100 to 140 characters Good for SEO')
.isLength({
min: 60,
max: 155
}),
check('postcontent', 'Fill Some Post Content').not().isEmpty().trim().escape(),
check('tag', 'Enter Atleast one Tag for Post').not().isEmpty().trim().escape(),
], function(req, res) {
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
const blog_title = req.body.title
const random_id = Math.floor(1000 + Math.random() * 9000)
const basename = sitedata.url_data + "-" + random_id
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(400).json(errors);
} else {
const seo_url = slugify(blog_title, {
replacement: '-',
remove: /[*+~.()'"!:@]/g,
lower: true,
strict: false
});
var title = blog_title;
var description = req.body.description;
var date = formattedDate;
var tag = req.body.tag;
var postcontent = req.body.postcontent;
let content = [{
title: title || "Example Post title",
description: description || "Example Post description",
date: date,
tag: tag || "Hello World",
postcontent: postcontent || "Example Post Content",
slug: decodeURIComponent(seo_url)
}];
let template = fs.readFileSync("./template.md").toString()
content.forEach(post_data => {
let output = render(template, post_data)
const clean_url = basename;
fs.writeFileSync(`${sitedata.storage_path}/${clean_url}.${sitedata.format}`, output)
console.log(post_data);
})
res.status(200).json({
sucess: 1,
message: 'Post Created'
});
}
});
app.get('/api', basicAuth({
users: { 'admin':sitedata.password },
challenge: true,
unauthorizedResponse: 'not authorized'
}), [
check('title', 'title length should be 50 to 60 characters Good for SEO')
.isLength({
min: 10,
max: 65
}),
check('description', 'description length should be 100 to 140 characters Good for SEO')
.isLength({
min: 60,
max: 155
}),
check('postcontent', 'Fill Some Post Content').not().isEmpty().trim().escape(),
check('tag', 'Enter Atleast one Tag for Post').not().isEmpty().trim().escape(),
], function(req, res) {
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
const blog_title = req.query.title
const random_id = Math.floor(1000 + Math.random() * 9000)
const basename = sitedata.url_data + "-" + random_id
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(400).json(errors);
} else {
const seo_url = slugify(blog_title, {
replacement: '-',
remove: /[*+~.()'"!:@]/g,
lower: true,
strict: false
});
var title = blog_title;
var description = req.query.description;
var date = formattedDate;
var tag = req.query.tag;
var postcontent = req.query.postcontent;
let content = [{
title: title || "Example Post title",
description: description || "Example Post description",
date: date,
tag: tag || "Hello World",
postcontent: postcontent || "Example Post Content",
slug: decodeURIComponent(seo_url)
}];
let template = fs.readFileSync("./template.md").toString()
content.forEach(post_data => {
let output = render(template, post_data)
const clean_url = basename;
fs.writeFileSync(`${sitedata.storage_path}/${clean_url}.${sitedata.format}`, output)
console.log(post_data);
})
res.status(200).json({
sucess: 1,
message: 'Post Created'
});
}
});
app.use('/', function(req, res) {
res.status(404).json({
error: 1,
message: 'Web App Error'
});
});
app.use(function(err, req, res, next) {
if (err.code !== 'EBADCSRFTOKEN') return next(err)
res.status(403).json({
error: 1,
message: 'Token Error'
});
})
module.exports = app;