Click here to Skip to main content
15,887,367 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to add a sitemap page in nodejs, but it's not working.

What I have tried:

I tried this but the page runs on a specific port only:
example.com:3001/sitemap.xml. However, I want to run it on example.com. I am confused, but running it on default HTTP 80 because nodejs web app process is already running on that port and it may create conflict.

JavaScript
const express = require('express');
const app = express();

// Serve the sitemap.xml file
app.get('/sitemap.xml', (req, res) => {
  res.sendFile('sitemap.xml', { root: __dirname });
});

// Other routes and middleware for your website...

// Start the server
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
Posted
Updated 2-Oct-23 5:16am
v2

1 solution

Your question makes no sense. Your app is listening on port 3000, so it will only respond to requests on port 3000. If you want it to respond to requests on port 80, then it needs to be listening on port 80.

If you already have a different application listening on port 80, then you need to add the file to that application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900