22102940
express 如何将 router.get(“/”,function({})); router.get(“/index”,function({})); router.get(“/index.html”,function({})); 写到一个路由处理上,而不是写三遍?
express 如何将 router.get(“/”,function({})); router.get(“/index”,function({})); router.get(“/index.html”,function({})); 写到一个路由处理上,而不是写三遍?
@1252198830 expressjs的官网router部分
@flamingtop 您好,请问这是什么操作 这个/:var() 简直666 表示并没有找到相关的文档说明(眼有点瞎) 可否给个相关说明文档链接 谢谢了 跪求
有的时候过度压缩代码也有坏处,我就写三遍。
app.get('/:var(index|index.html)?', fn)
同楼上,正则匹配是最简单的,如果自己玩的话可以获取路径switch选择
router.get(['/','/index.html'],function({}));
感觉没必要支持3个一样的路由,直接支持 / 结束,又不是静态的,没必要再加个index或index.html
感觉正则不如写三遍 哈哈哈
就 router 来说, 他是只支持一个 path 的 https://github.com/expressjs/express/blob/4.13.4/lib/router/index.js#L509
伪静态化~哈哈
首先正则是可以的 app.get(/^\/(index(\.html)?)?/, function(){ ... })
正则
正则匹配
你这个完全不需要路由啊,把index.html放到public文件夹下,然后设置:
app.use(express.static('public'))
我来写的话,就写三遍。