Skip to content

Commit

Permalink
添加上海&长沙
Browse files Browse the repository at this point in the history
  • Loading branch information
巴思 committed Nov 6, 2014
1 parent 0e4a683 commit 88c4f35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
58 changes: 22 additions & 36 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ var hbs = exphbs.create({
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

var cities = [
{key: 'hangzhou', name: '浙江杭州'},
{key: 'shanghai', name: '上海'},
{key: 'beijing', name: '北京'},
{key: 'chengdu', name: '四川成都'},
{key: 'nanning', name: '广西南宁'},
{key: 'changsha', name: '湖南长沙'}
];

app.get('/', function (req, res, next) {
res.render('home');
res.render('home', {cities: cities});
});

// 针对各个地域的 route 配置
Expand All @@ -36,45 +45,22 @@ app.get('/all', function (req, res, next) {
});
});

app.get('/hangzhou', function (req, res, next) {
Post.find({author_location: '浙江杭州'}).sort({create_at: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
res.render('posts', {docs: docs});
});
});

app.get('/chengdu', function (req, res, next) {
Post.find({author_location: '四川成都'}).sort({create_at: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
res.render('posts', {docs: docs});
});
});

app.get('/beijing', function (req, res, next) {
Post.find({author_location: '北京'}).sort({create_at: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
res.render('posts', {docs: docs});
});
});
for (var i = 0; i < cities.length; i++) {
(function (city) {
app.get('/city/' + city.key, function (req, res, next) {
Post.find({author_location: city.name}).sort({create_at: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
res.render('posts', {docs: docs});
});
});
})(cities[i]);
}

app.get('/nanning', function (req, res, next) {
Post.find({author_location: '广西南宁'}).sort({create_at: -1}).limit(100).exec(function (err, docs) {
if (err) {
return next(err);
}
res.render('posts', {docs: docs});
});
});
// END 针对各个地域的 route 配置



// 启动爬虫
crawler.start();

Expand Down
7 changes: 3 additions & 4 deletions views/home.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
rel="nofollow noreferrer">请不要嘿咻</a></h3>
<p>选择地域:</p>
<p><a href="/all">所有</a></p>
<p><a href="/hangzhou">浙江杭州</a></p>
<p><a href="/nanning">广西南宁</a></p>
<p><a href="/chengdu">四川成都</a></p>
<p><a href="/beijing">北京</a></p>
{{#each cities}}
<p><a href="/city/{{key}}">{{name}}</a></p>
{{/each}}

0 comments on commit 88c4f35

Please sign in to comment.