-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
49 lines (40 loc) · 1.09 KB
/
sitemap.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
var sm = require('sitemap')
, Post = require('./models/post').Post
, config = require('../../config.js')
, host = config.production.url || config.development.url
, when = require('when');
/*
* 绑定路由
*/
module.exports = function(server){
server.get('/sitemap.xml', function(req, res){
var urls = ['/'];
// 发送响应xml
function response(xml){
res.header('Content-Type', 'application/xml');
res.send(xml)
}
// 查询model
Post.findAll({columns: ['id', 'slug']}).then(function(posts){
urls = urls.concat(posts.models.map(function(p){
return '/' + p.attributes.slug
}));
createSitemap(urls, response);
}).otherwise(function(err){
createSitemap(urls, response);
});
});
}
/*
* sitemap创建方法
*/
function createSitemap(urls, cbk){
var sitemap = sm.createSitemap ({
hostname: host,
cacheTime: 600000,
urls: urls
});
sitemap.toXML( function (xml) {
cbk(xml);
});
}