-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (86 loc) · 3.06 KB
/
index.js
File metadata and controls
92 lines (86 loc) · 3.06 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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const FuzzySet = require('fuzzyset');
//const keep_alive = require('./keep_alive.js')
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Express api is awake. Usage: /api/addanswer/(id)/(question)/(answer)/ or /api/getdata/ or /api/search/(search query)');
});
app.get('/api/addanswer/:Id/:question/:answer/', (req, res) => {
try{
fs.readFile("questions_and_answers.json", 'utf8', function(err, data) {
if (err) throw err;
var datajson = JSON.parse(data);
if (!(typeof datajson[req.params.Id] === 'object' && datajson[req.params.Id] !== null)) {//adds id if doesnt already exist as object
datajson[req.params.Id] = {};
}
datajson[req.params.Id][req.params.question] = req.params.answer;
var dataupdated = JSON.stringify(datajson);
fs.writeFile("questions_and_answers.json", dataupdated, function(err) {
if (err) return console.log(err);
res.send("id: " + req.params.Id + "\nadded question: " + req.params.question + "\nwith answer: " + req.params.answer);
});
});
}catch(Err){
console.log(Err);
}
});
app.get('/api/getdata/', (req, res) => {
try{
fs.readFile("questions_and_answers.json", 'utf8', function(err, data) {
if (err) return console.log(err);
res.send(data);
//console.log(data);
});
}catch(Err){
console.log(Err);
}
});
app.get('/api/search/:id/:question', (req, res) => {
try{
fs.readFile("questions_and_answers.json", 'utf8', function(err, data) {
if (err) return console.log(err);
a = FuzzySet();
var questions=JSON.parse(data)[req.params.id];
for(var question in questions)a.add(question);
//questions.forEach((i)=>{a.add(i)});
var searchResult=a.get(req.params.question,"no match",0.33);//query,default,minimum
//var answer=encodeURIComponent(searchResult[0][1]);
var question, confidence,tempJSON;
if(searchResult!=="no match"){
question=searchResult[0][1];
confidence=searchResult[0][0];
tempJSON=JSON.parse("{}");
tempJSON.question=question;
tempJSON.answer=questions[question];
tempJSON.confidence=confidence;
}
res.send((searchResult=="no match")?searchResult:JSON.stringify(tempJSON));
//console.log(data);
});
}catch(Err){
console.log(Err);
}
});
app.get('/api/search/:id', (req, res) => {
try{
fs.readFile("questions_and_answers.json", 'utf8', function(err, data) {
if (err) return console.log(err);
var questions=JSON.parse(data)[req.params.id];
//questions.forEach((i)=>{a.add(i)});
res.send(JSON.stringify(questions));
//console.log(data);
});
}catch(Err){
console.log(Err);
}
});
app.get('/api/test', (req, res) => {
res.send("hello world!");
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log('server started'));