-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (76 loc) · 2.16 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
var path = require('path');
var express = require('express');
var cloud = require('./data/leancloud');
var cron = require('./data/cron');
var config = require("./config");
var app = express();
var search = require("./data/search/search");
var LIST;
cron.job.start(); // start crontribute
cloud.getListByPage().then(
function(res){
LIST = res
},
function(error){
console.log("Error : getListByPage " + error);
}
);
// 配置 express
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use('/web', express.static(path.join(__dirname, 'web')));
// 网站首页
app.get('/', function(req, res, next){
// articleListByClassId 的第一个参数是文章分类的 ID
// 第二个参数是返回结果的开始位置
// 渲染模板
res.locals.List = LIST;
res.locals.moment = require('moment');
res.render('index');
cloud.getListByPage().then(
function(res){
LIST = res
},
function(error){
console.log("Error : getListByPage " + error);
}
);
next();
});
// 翻页
app.get('/page/:id', function(req, res, next){
res.locals.moment = require('moment');
cloud.getListByPage(req.params.id).then(
function(response){
res.locals.List = response;
res.render('pagelist');
next();
},
function(error){
console.log("Error : getListByPage " + error);
}
);
});
// 分页json数据
app.get('/list/:id', function(req, res, next){
res.locals.moment = require('moment');
cloud.getListByPage(req.params.id).then(
function(response){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(response));
next();
},
function(error){
console.log("Error : getListByPage JSON" + error);
}
);
});
// 搜索数据
app.get('/search/:word', function(req, res, next){
res.setHeader('Content-Type', 'application/json');
res.send(search.search(req.params.word));
next();
});
app.listen(config.port, function(){
console.log('服务器已启动 监听3000端口');
});