Simplifying Sitemap Creation Using Node.js

⏱ 4 min read

Imagine building a stunning website packed with valuable content — but it’s hidden in the dark, because search engines don’t know where to find it. That’s exactly what happens if you don’t have a sitemap.

A sitemap is your site’s roadmap, telling Google, Bing, and others exactly what pages you have so they can crawl, index, and rank your content properly. Without it, your website is like a ship lost at sea without a compass.


Why You Absolutely Need a Sitemap

Building a website without a sitemap and submitting it to search engines is like waiting for a MAANG job without ever applying — you’re missing the essential first step.


Introducing ap-sitemap — Your Node.js Sitemap Generator

To make sitemap creation a breeze, I built an npm package called ap-sitemap. It’s simple, powerful, and built for Node.js projects of all sizes.

Key Features


How to Get Started

Step 1: Install the Package

npm install ap-sitemap

Step 2: Create Your Sitemap Script

Here’s a full example script you can customize:

// Import the package
const SiteMapGenerator = require('ap-sitemap');

// Initialize with your config
const sitemap = new SiteMapGenerator({
  baseUrl: 'https://amanpareek.in',  // Your website URL here
  outDir: 'dist',                    // Output folder for sitemap files
  limit: 50000,                     // Max URLs per sitemap file
  removeIndexExtension: true,       // Clean URLs for better SEO
});

// Add pages to your sitemap
sitemap.addPages([
  {
    url: 'https://amanpareek.in/page1',
    updatedAt: '2024-11-04T10:00:00Z',
    changefreq: 'daily',
    priority: 1.0,
  },
  {
    url: 'https://amanpareek.in/page2',
    updatedAt: '2024-11-03T10:00:00Z',
    changefreq: 'weekly',
    priority: 0.8,
  },
  // Add more pages as needed
]);

// Generate sitemap files
const sitemapUrl = sitemap.generate();

// Log the sitemap URL for robots.txt (public URL, NOT including local folder like 'dist')
console.log('✅ Sitemap generated!');
console.log('Add this to your robots.txt:', sitemapUrl);

Step 3: Update Your robots.txt File

Make sure to add the sitemap URL to your robots.txt file so search engines can find it easily.

User-agent: *
Allow: /

Sitemap: https://amanpareek.in/sitemap.xml

Note: The sitemap files are generated locally inside the dist/ folder, but the URL you provide in robots.txt should be the public URL where the sitemap is accessible on your website.


Fetch URLs from MongoDB or Any Database Easily

In real-world applications, your pages or posts usually come from databases like MongoDB, PostgreSQL, or even external APIs. You can fetch those URLs dynamically, transform them into the format that ap-sitemap requires, and generate your sitemap automatically.

Example: Fetching Pages from MongoDB

const SiteMapGenerator = require('ap-sitemap');
const mongoose = require('mongoose');

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/your-db', { useNewUrlParser: true, useUnifiedTopology: true });

// Define your Page schema (example)
const pageSchema = new mongoose.Schema({
  slug: String,
  updatedAt: Date,
});

const Page = mongoose.model('Page', pageSchema);

async function generateSitemap() {
  const sitemap = new SiteMapGenerator({
    baseUrl: 'https://amanpareek.in',
    outDir: 'dist',
    limit: 50000,
    removeIndexExtension: true,
  });

  // Fetch pages from DB
  const pages = await Page.find().exec();

  // Map DB documents to sitemap format
  const sitemapPages = pages.map(page => ({
    url: `https://amanpareek.in/${page.slug}`,
    updatedAt: page.updatedAt.toISOString(),
    changefreq: 'weekly',
    priority: 0.8,
  }));

  sitemap.addPages(sitemapPages);

  const sitemapUrl = sitemap.generate();

  console.log('✅ Sitemap generated!');
  console.log('Add this URL to your robots.txt:', sitemapUrl);
}

generateSitemap()
  .then(() => mongoose.disconnect())
  .catch(err => {
    console.error('Error generating sitemap:', err);
    mongoose.disconnect();
  });

This way, your sitemap stays always up to date with your live data — no manual work required!


Pro Tips for Maximum SEO Impact


Who Should Use ap-sitemap?


Summary

Creating and submitting a sitemap is not optional if you want your site to rank and be found. With ap-sitemap, you get a fast, reliable way to generate professional sitemaps in minutes, no matter your site size.

Don’t leave your site lost in the wild — give search engines a clear path to your content today!