NodeJS Method Example for Generating SiteMap Site Map

If the blog is managed using HEXO, SiteMap can use the plugin to generate. But for a content management website, the backend may be an Express, Koa’s framework. At this time, SiteMap needs you to generate

What is SiteMap

SiteMap can easily notify Search Engines which are available on their website. The simplest SiteMap form is the XML file where the URL in the website is listed and other metadata on each URL (the time, the frequency of the change, the frequency of the change, and the other URLs on the website), etc.) So so that the search engine can more intelligently grabbed the website.

SiteMap structure
  2019-01        LOC: Article Link address   LastMod: Last Update   Changefreq: DAILY / MONthly 
Priority: Weight
  • Generate SiteMap Based on Express project
  • Open source package: Sitemap, address:

> NPM Install –save SiteMap

const express = required (‘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: articles ({HostName: ‘http: / / ‘, 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. extend (XML);};});});
   SiteMap optimization 

above The method can be used when the number of articles is small, and if there are thousands or even tens of thousands of articles, the way to pull it is not suitable, just write the return result into the file, updated one day. It only needs to be changed to

SiteMap.Toxml ()

fs.writeFileSync (“app / assets /sitemap.xml, SiteMap.Tostring ()); . Each time you request SiteMap
The above is the full content of this article, I hope that everyone’s learning is helpful, I hope everyone will support Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment