-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
93 lines (80 loc) · 2.35 KB
/
server.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
const express = require('express')
const jsonfile = require('jsonfile');
const app = express()
const FILE_OUTPUT = __dirname + '/result.json';
var port = process.env.PORT || 8080;
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/static'));
app.get('/', function(req, res) {
res.render('index');
});
app.get('/result', function(req, res) {
data = jsonfile.readFileSync(FILE_OUTPUT);
let length = data['data'].length;
let arr = [];
let page = 1;
let offset = 20;
if (req.query.page) {
page = req.query.page;
}
if (page * offset > length && (page - 1) * offset > length)
res.send({});
let run = page * offset > length ? length : page * offset;
for (let i = (page - 1) * offset; i < run; i++) {
let t = data['data'][i];
t['offset'] = i;
arr.push(t);
}
// res.render('article', arr);
res.send(arr);
})
app.get('/articles/:id/:slug', function(req, res) {
data = jsonfile.readFileSync(FILE_OUTPUT);
let slug = "";
let id = -1;
if (req.params.slug && req.params.id) {
slug = req.params.slug;
id = req.params.id;
} else
res.send({ 'msg': 'Input invalid' });
let arr = {};
if (id != -1)
arr = data['data'][id];
res.render('article', arr);
// res.send(arr);
})
app.get('/articles/:id/:slug/result', function(req, res) {
data = jsonfile.readFileSync(FILE_OUTPUT);
let slug = "";
let id = -1;
if (req.params.slug && req.params.id) {
slug = req.params.slug;
id = req.params.id;
} else
res.send({ 'msg': 'Input invalid' });
let arr = {};
if (id != -1)
arr = data['data'][id]['link_image'];
// let page = 1;
// let offset = 20;
// if (req.query.page) {
// page = req.query.page;
// }
// if (page * offset > length && (page - 1) * offset > length)
// res.send({});
// let run = page * offset > length ? length : page * offset;
let dat = [];
// console.dir(arr);
for (let i = 0; i < arr.length; i++) {
let a = {};
a['link'] = arr[i];
dat.push(a);
}
// console.log(dat);
// res.render('article', arr);
res.send(dat);
})
// app.get('/home', function(req, res) {
// res.send('hello world')
// })
app.listen(port, () => console.log('Server app listening on port:' + port));