任何方式提供静态html文件从快递没有扩展名?

编程入门 行业动态 更新时间:2024-10-22 23:07:19
本文介绍了任何方式提供静态html文件从快递没有扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想提供一个html文件,而不指定它的扩展名。有没有什么办法可以做到这一点没有定义一条路线?例如,而不是

I would like to serve an html file without specifying it's extension. Is there any way I can do this without defining a route? For instance instead of

/helloworld.html

我想做的只是

/helloworld

推荐答案

一个快速的解决方案是附加 .html 到没有期限的公开目录中存在HTML文件的请求:

A quick'n'dirty solution is to attach .html to requests that don't have a period in them and for which an HTML-file exists in the public directory:

var fs = require('fs'); var publicdir = __dirname + '/public'; app.use(function(req, res, next) { if (req.path.indexOf('.') === -1) { var file = publicdir + req.path + '.html'; fs.exists(file, function(exists) { if (exists) req.url += '.html'; next(); }); } else next(); }); app.use(express.static(publicdir));

更多推荐

任何方式提供静态html文件从快递没有扩展名?

本文发布于:2023-11-17 07:22:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扩展名   静态   快递   方式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!