我正在尝试使用the npm sitemap
package的v5.1.0创建站点地图索引。
我已经有了可以创建站点地图的工作代码:
const { createSitemap } = require('sitemap')
function getSitemap(hostname,urls) {
const s = createSitemap({
hostname,urls,cacheTime: config.SITEMAP_CACHE_TIME_SECS * 1000,});
return s.toXML(function(err,xml) {
if (err) return reject(err);
resolve(xml);
});
}
exports.routes = function(app) {
app.get("/sitemap.xml",function(req,res) {
Promise.all([
entity.Template.allPublicShowcaseURLs(),entity.User.allPublicShowcaseURLs(),]).then(([showcase_urls,profile_urls]) => {
return getSitemap(config.URL,showcase_urls.concat(profile_urls));
})
.then(serveXML(res))
.catch(utils.fail(req,res));
});
};
现在,我正在尝试更改它,以创建the documentation之后的站点地图索引(这并不是很清楚)。
这是我要尝试的:
const { createSitemapsAndIndex } = require('sitemap')
function getSitemap(hostname,urls) {
const smi = createSitemapsAndIndex({
hostname,targetFolder: require('os').tmpdir()
});
console.log('smi',smi)
return smi.toXML(function(err,xml) {
if (err) return reject(err);
resolve(xml);
});
}
但是此操作失败,smi.toXML is not a function
。控制台显示此时smi
是Promise { <pending> }
。我认为此函数可能返回Promise,因为它想向文件系统(?)写入内容。我去过trying to read the source,但无法真正跟随它。
我想我需要解决Promise,获取它返回的任何内容,然后将其转换为XML-但是如何?