-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapp.js
128 lines (113 loc) · 3.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
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
/**!
* haixiu - app.js
*
*/
'use strict';
/**
* Module dependencies.
*/
const express = require('express');
const exphbs = require('express-handlebars');
const mongoose = require('mongoose');
const crawler = require('./crawler');
const model = require('./model');
const Post = model.Post;
const config = require('./config');
mongoose.connect(config.mongodb_url);
let app = express();
let hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
gaid: function () {
return config.gaid;
},
},
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
let cities = [
{key: 'hangzhou', name: '浙江杭州'},
{key: 'shanghai', name: '上海'},
{key: 'beijing', name: '北京'},
{key: 'chengdu', name: '四川成都'},
{key: 'nanning', name: '广西南宁'},
{key: 'changsha', name: '湖南长沙'},
{key: 'changsanjiao', name: '长三角', names: [
'浙江杭州', '浙江温州', '浙江宁波', '浙江台州',
'浙江嘉兴', '浙江金华', '浙江绍兴', '浙江湖州',
'浙江丽水', '浙江衢州', '浙江舟山', '上海', '江苏南京'
]
},
{key: 'guangzhou', name: '广东广州'},
{key: 'shenzhen', name: '广东深圳'},
];
function fixImages(imgs) {
imgs = imgs || [];
return imgs.map(function (img) {
if (img && img.startsWith('https://')) {
img = img.replace('https://', 'http://');
}
img = img.replace('.doubanio.com', '.douban.com');
return img;
});
}
function fixDocs(docs) {
docs = docs || [];
return docs.map(function (doc) {
doc.imgs = fixImages(doc.imgs);
return doc;
});
}
app.get('/', function (req, res, next) {
res.render('home', {cities: cities});
});
// 针对各个地域的 route 配置
app.get('/all', function (req, res, next) {
Post.find().sort({id: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
docs = fixDocs(docs);
res.render('posts', {docs: docs});
});
});
for (let i = 0; i < cities.length; i++) {
(function (city) {
let names = city.names || [city.name];
app.get('/city/' + city.key, function (req, res, next) {
Post.find({author_location: {$in: names}}).sort({id: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
docs = fixDocs(docs);
res.render('posts', {docs: docs});
});
});
})(cities[i]);
}
// END 针对各个地域的 route 配置
// 某个用户的发帖
app.get('/author/:authorId', function (req, res, next) {
const authorId = req.params.authorId;
Post.find({author_id: authorId}).sort({id: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
let authorName = '';
if (docs && docs.length) {
// 取最近一条帖子的昵称
authorName = docs[0].author_name;
}
docs = fixDocs(docs);
res.render('author', {
authorId: authorId,
authorName: authorName,
docs: docs,
});
});
});
// 启动爬虫
crawler.start();
let server = app.listen(config.port, function () {
console.log('app is listening ' + server.address().port);
});