This repository was archived by the owner on Feb 8, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (103 loc) · 3.05 KB
/
Copy pathapp.js
File metadata and controls
122 lines (103 loc) · 3.05 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
116
117
118
119
120
121
122
var express = require('express'),
app = express(),
// router = express.Router(),
port = process.env.PORT || 3000,
fs = require('fs');
var bodyParser = require('body-parser');
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.listen(port, function () {
console.log("Live at Port " + port);
});
var elasticlunr = require('elasticlunr');
var idx = elasticlunr(function () {
this.setRef('id');
this.addField('title');
this.addField('body');
});
fs.readFile('articles.json', function (err, data) {
if (err) throw err;
var raw = JSON.parse(data);
// console.log(raw);
var articles = raw.map(function (a) {
return {
id: a.id,
title: a.data.title,
body: a.data.markdown
};
});
articles.forEach(function (article) {
idx.addDoc(article);
});
fs.writeFile('index.json', JSON.stringify(idx), function (err) {
if (err) throw err;
console.log('index created');
});
});
module.exports = idx;
// console.log(index.search("example"));
app.post("/endpoint", function (req, res) {
var json = JSON.stringify(req.body);
// console.log(json);
var obj = JSON.parse(json);
// console.log(obj);
articleData = obj.articles;
// console.log(articleData);
var doc = {};
doc.id = articleData.id;
doc.title = articleData.data.title;
doc.body = articleData.data.markdown;
idx.addDoc(doc);
// console.log(JSON.stringify(idx));
// console.log(doc);
fs.readFile('articles.json', function (err, d) {
var data = JSON.parse(d);
// console.log("data:");
// console.log(data);
data.push(articleData);
articles = JSON.stringify(data);
// console.log(articles);
fs.writeFile("articles.json", articles, function (err) {
if (err) {
// there was an error :(
console.log("error buddy: " + err);
} else {
// yeet then yote, this was a go
}
});
});
});
app.post("/articleSender", function (req, res) {
});
// var router = express.Router();
// var path = __dirname + '/';
// app.post('/articles.json', function (req, res) {
// // console.log(req);
// console.log('req received');
// data = JSON.stringify(req.body)
// fs.writeFile("articles.json", data);
// // res.redirect('/');
// });
// router.use(function (req, res, next) {
// console.log("/" + req.method);
// next();
// });
// router.get("/", function (req, res) {
// res.sendFile(path + "index.html");
// });
// router.get("/writeArticle", function (req, res) {
// res.sendFile(path + "articleEditor.html");
// });
// router.get("/articles.json", function (req, res) {
// res.sendFile(path + "articles.json");
// });
// router.post("/articles.json", function (req, res) {
// res.sendFile(path + "articles.json");
// });
// app.use("/", router);
// app.use("*", function (req, res) {
// res.sendFile(path + "404.html");
// });