简体   繁体   English

使用Express服务静态文件

[英]Serve static files with Express

I'm trying to add a sitemap to a Express project with EJS set as it's engine. 我正在尝试将EJS设置为引擎的站点地图添加到Express项目中。

The problem is my app does not allow a .html or .xml file to be in the path because of express. 问题是我的应用程序由于express不允许将.html或.xml文件放在路径中。

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? 我将如何简单地添加一条路由以允许.xml渲染?

Thanks! 谢谢!

Ah figured it out with the help of: Ah借助以下方法找出了答案:

In nodejs/express how to allow sitemap for crawler 在Node.js / Express中如何允许搜寻器使用Sitemap

I created the sitemap.xml file in the public folder of my project then used this route: 我在项目的公用文件夹中创建了sitemap.xml文件,然后使用以下路线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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