简体   繁体   中英

Serve static files with Express

I'm trying to add a sitemap to a Express project with EJS set as it's engine.

The problem is my app does not allow a .html or .xml file to be in the path because of express.

I tried to set a public path but this crashed my app and seemed overly complicated:

app.use(express.static(path.join(__dirname, 'public')));

How would I go about simply adding a route to allow a .xml render?

Thanks!

Ah figured it out with the help of:

In nodejs/express how to allow sitemap for crawler

I created the sitemap.xml file in the public folder of my project then used this route:

router.get("/sitemap.xml", function(req, res, next){
  res.sendFile(__dirname + '/public/sitemap.xml'); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM