内容简介:如果博客是使用Hexo管理的,sitemap可以使用插件来生成。但对于一个内容管理网站,后端可能是express、koa之类的框架,这时sitemap就需要自己来生成了Sitemap可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的Sitemap形式,就是XML文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。loc:文章链接地址
如果博客是使用Hexo管理的,sitemap可以使用插件来生成。但对于一个内容管理网站,后端可能是express、koa之类的框架,这时sitemap就需要自己来生成了
什么是sitemap
Sitemap可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的Sitemap形式,就是XML文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。
sitemap结构
<url>
<loc>http://www.jouypub.com/</loc>
<lastmod>2019-05-01</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
loc:文章链接地址
lastmod:最后更新时间
changefreq:更新频率,daily/monthly
priority:权重
生成sitemap,基于express项目
开源包:sitemap,地址: https://github.com/ekalinin/s...
> npm install --save sitemap
代码中使用
const express = require('express')
const sm = require('sitemap');
router.get('/sitemap.xml', function (req, res) {
let pageRequest = Object.create({});
pageRequest.pageSize = -1;
pageRequest.pageNum = 1;
api.post('/article/list', pageRequest, function (result) {
let urls = [];
for (let article in result) {
urls.push({
url: article.url,
changefreq: 'daily',
lastmodrealtime: true,
priority: 1,
lastmod: article.updateTime
});
}
let sitemap = sm.createSitemap({
hostname: 'http://invest.jouypub.com',
cacheTime: 600000, // 600sec, cache purge period
urls: urls
});
sitemap.toXML(function (err, xml) {
if (err) {
console.log(err);
return res.status(500).end();
}
res.header('Content-Type', 'application/xml');
res.send(xml);
});
});
});
sitemap优化
上面那种方法在文章数少时还能使用,如果有几千甚至几万篇文章,一次拉取的方式就不适合了,就需要把返回结果写入到文件中,一天更新一次。只需要只需要把
sitemap.toXML()
改成
fs.writeFileSync("app/assets/sitemap.xml", sitemap.toString()); 即可。每次请求sitemap时读文件即可
欢迎订阅「K叔区块链」 - 专注于区块链技术学习
博客地址: http://www.jouypub.com
简书主页: https://www.jianshu.com/u/756c9c8ae984
segmentfault主页: https://segmentfault.com/blog/jouypub
腾讯云主页: https://cloud.tencent.com/developer/column/72548以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Jekyll 3.6.1 发布,静态站点生成器
- Jekyll 3.7.0 发布,静态站点生成器
- Jekyll 3.7.3 发布,静态站点生成器
- Jekyll v3.8.1 发布,静态站点生成器
- 生成根证书CA及签发子证书及配置站点实践
- Jekyll v3.8.2 发布,简单易用的静态站点生成器
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Haskell Programming from first principles
Christopher Allen、Julie Moronuki / 2015 / USD 59.00
I am writing this book because I had a hard time learning Haskell. It doesn't have to be that way. I've spent the last couple years actively teaching Haskell online and in person. Along the way, I ......一起来看看 《Haskell Programming from first principles》 这本书的介绍吧!