Ask Question

How implement sitemap api for google in nodejs express

I have a web application which is running with nodejs + express and want to provide an sitemap api for google search console.
As far as I see there are multiple sitemap protocol, but I think I will go with the simple text based format. (See more)

The text file should contain one URL per line. For example:

Additionally fullfil those guidelines for text file sitemaps:

  • Encode your file using UTF-8 encoding.
  • Your text file should contain nothing but the list of URLs.
  • You can name the text file anything you wish, provided it has a .txt extension (for instance, sitemap.txt).


AuthorΒ΄s Anki Batsukh image

Anki Batsukh

Last edited on

1 Answer available

Best answer

The following code-snipped should work πŸ˜‰ Since you are delivering just a text content the response header property content-type should be text/plain

// create router for sitemap
const sitemapRouter = express.Router();
sitemapRouter.get('/api/sitemap.txt', async function (req, res) {
  const links = await getAllLinkData();
  res.set('content-type', 'text/plain');
  return res.send( => link.url).join('\n'));

Last edited on