What is sitemap
sitemap, the site address file, is usually named sitemap.xml.
What is the use of sitemap?
Sitemap is very friendly to search engines. If new files are generated on your website, using sitemap can allow search engines to quickly include them, which is the most commonly used method for website SEO.
How is the sitemap file generated?
Sitemap files can be generated by tool software, and there are also websites that provide paid services for online generation.
Today, we are programming to generate the sitemap.xml file ourselves.
Of course, you first need to understand the format of the sitemap file.
Domestically, the most authoritative one is Baidu. We also submit sitemaps to it. The sitemap file format given by Baidu is as follows:
According to this introduction, we use NodeJS coding as follows:
var sitemap_data = ""; var fs = require ("fs"); // Page directory fs.readdir ("./ article /", function (error, files) {// Sitemap fixed format, coding setting sitemap_data = sitemap_data + '<? xml version = "1.0" encoding = "utf-8"?>' + "\ r \ n"; sitemap_data = sitemap_data + '<urlset>' + "\ r \ n"; // Get each The article, and write it as a limited format for (i = 0; i <files.length; i ++) { sitemap_data = sitemap_data + '<url>' + "\ r \ n"; sitemap_data = sitemap_data + '<loc>' + " Change to url address here "+ files [i] + '</ loc>' +" \ r \ n "; sitemap_data = sitemap_data + '<lastmod> 2020-04-12 </ lastmod>' +" \ r \ n "; sitemap_data = sitemap_data + '</url>' + "\r\n"; } sitemap_data = sitemap_data + '</urlset>'; // Write in sitemap.xml format fs.writeFileSync("sitemap.xml",sitemap_data); })
Run, generate sitemap.xml file as follows:
Submit sitemap
Submit to your website:
Then go to Baidu webmaster platform and submit to Baidu:
In this way, Baidu Crawler will quickly crawl related files to complete the collection. Then, naturally, the file can be searched.
Original articles, unlimited reprint. Welcome to share learning, everyone progress together.