-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
148 lines (144 loc) · 4.25 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var koa = require('koa');
var app = koa();
var controller = require('koa-route');
var qs = require('querystring');
//设置模板引擎
var views = require('co-views');
var render = views('./view', {//设置路径和要使用的模板引擎
map : { html : 'ejs'}
});
//引入service模块,他是前后端数据交互的纽带
var service = require('./service/service.js');
//提供静态资源服务
var koa_static = require('koa-static-server');
app.use(koa_static({
rootDir : './static', //文件的位置
rootPath : '/static', //url的路径
maxage : 0 //不缓存
}));//使用到中间件,要放到app.use()里面
//为所有页面做的路由如下
//首页
app.use(controller.get('/', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('index', {
title : '书城首页'
});
}));
//搜索页
app.use(controller.get('/search', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('search', {
title: '搜索'
});
}));
//女生页
app.use(controller.get('/female', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('female', {
title : '女生频道',
concern : '最受关注',
recommend : '主编推荐',
news : '新书入库',
over : '完结好书'
});
}));
//男生页
app.use(controller.get('/male', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('male', {
title : '男生频道',
concern : '最受关注',
recommend : '主编推荐',
news : '新书入库',
over : '完结好书'
});
}));
//排行页
app.use(controller.get('/rank', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('rank', {
title: '书籍排行'
});
}));
//种类页
app.use(controller.get('/category', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('category', {
title : '分类',
concern : '最受关注',
recommend : '主编推荐',
news : '新书入库',
over : '完结好书'
});
}));
//书籍详情页
app.use(controller.get('/book', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('book', {
title : '书籍详情'
});
}));
//阅读器页
app.use(controller.get('/reader', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield render('reader', {
title: '内容阅读'
});
}));
//为ajax请求做的路由如下
//获得搜索信息
app.use(controller.get('/ajax/search', function*(){
var queryObj = qs.parse(this.req._parsedUrl.query);
var keyword = queryObj.keyword;
var start = queryObj.start;
var end = queryObj.end;
this.body = yield service.get_search_data(start, end, keyword);
}));
//获得首页信息
app.use(controller.get('/ajax/index', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_index_data();
}));
//获得排序信息
app.use(controller.get('/ajax/rank', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_rank_data();
}));
//种类信息
app.use(controller.get('/ajax/category', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_category_data();
}));
//获得女生信息
app.use(controller.get('/ajax/female', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_female_data();
}));
//获得男生信息
app.use(controller.get('/ajax/male', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_male_data();
}));
//获得book信息
app.use(controller.get('/ajax/book', function*(){
var id = this.query.id;
this.set('Cache-Control', 'no-cache');
if(!id){
id = '';
}
//模拟数据
id = '18218';
this.body = yield service.get_book_data(id);
}));
//获得章节id
app.use(controller.get('/ajax/chapter', function*(){
this.set('Cache-Control', 'no-cache');
this.body = yield service.get_chapter_data();
}));
//获得章节信息
app.use(controller.get('/ajax/chapter/data', function*(){
var pageId = this.query.page;
this.body = yield service.get_data_data(pageId);
}));
//监听端口
app.listen(5050);