-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (85 loc) · 2.09 KB
/
app.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
const express = require('express');
const nunjucks = require('nunjucks');
const path = require('path');
const request=require('request');
const Datastore = require('nedb');
const PORT = 8080;
const app = express();
db = new Datastore({filename:path.join(__dirname, 'static/test.db'),
autoload: true});
app.use(express.static(path.join(__dirname, 'static')));
nunjucks.configure('templates', {
autoescape: true,
express: app
});
app.get('/', (req,res) => {
res.redirect("/html/Menu.html")
})
app.get('/reviews', (req,res) => {
res.redirect("/html/reviews.html")
})
app.get('/news', (req,res) => {
request
.get('http://localhost:8080/api/news/Title?name='+req.query.Name)
.on('data', (data) => {
results = JSON.parse(data)
console.log("Rex Sneeze")
console.log(data)
res.render("news.html", {'data': results})
})
});
app.get("/test", (req,res) => {
request
.get('http://localhost:8080/api/news/id?name='+req.query.Name)
.on('data', (data) => {
data2 = JSON.parse(data)
console.log("Rex Sneeze")
console.log(data2)
res.render("test.html", {'data2': data2})
})
});
app.get('/api/news/id', (req,res) =>{
let name = req.query.name;
let ffs = [];
db.find({_id: name}, function (err,docs) {
var data = docs
console.log(data)
res.status(200).json(docs)
})
console.log(ffs)
});
app.get('/api/news', (req,res) =>{
let name = req.query.name;
let ffs = [];
db.find({}, function (err,docs) {
var data = docs
console.log(data)
data.forEach((item) => {
if (item._id == name) {
ffs.push(item);
} else if (item.Title.toLowerCase().indexOf(name.toLowerCase()) >= 0) {
ffs.push(item);
}
})
console.log(ffs)
res.status(200).json(ffs)
});
})
app.get('/api/news/title', (req,res) =>{
let name = req.query.name;
if (!name) {
name = '';
}
var data = new RegExp(name)
console.log(data);
db.find({title: data}, function (err,docs) {
console.log(docs)
res.status(200).json(docs)
});
});
app.get('*', function(req, res) {
res.redirect('/html/error.html');
});
const server = app.listen(PORT, () =>{
console.log('Server listing on', server.address());
});