javascript - app.use ("/", express.static) 和 app.use(express.static) 之间有区别吗?

标签 javascript node.js express

假设我们首先调用了 app.set('thePath', thePath),下面的内容有区别吗?

  1. app.use('/', express.static(thePath))
  2. app.use(express.static(thePath))
  3. app.use(express.static(app.get('thePath')))

似乎 (1) 和 (2) 会做同样的事情。

据我所知,在 Javascript 中,如果你调用一个函数,它会在调用时计算,所以即使 app.get('thePath') 改变或者如果 thePath 更改,所有这些都将保持不变。

如果有不同,请告诉我是什么以及为什么。

最佳答案

http://expressjs.com/en/4x/api.html#app.use

If path is not specified, it defaults to “/”.

所以是的,1 和 2 是一样的。假设 thePath 是一个指向静态 Assets 有效根目录的变量。

关于javascript - app.use ("/", express.static) 和 app.use(express.static) 之间有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735358/

相关文章:

node.js - 为什么在路由中调用时我的 Passport 身份验证函数不执行?

node.js - 来自 NodeJS 的空 POST 请求?

javascript - jQuery 的神秘情况还是我瞎了眼?

javascript - Passport.js:使用 Github 登录并返回电子邮件地址

node.js - Express API 集成测试 : Error: timeout of 2000ms exceeded. 确保在此测试中调用 done() 回调

node.js - 在 Nginx 子路径上部署 Webpack

javascript - 在 router.get 中使用类方法

javascript - 原始 HTML-选择 : removing option will show selection

javascript - Three.js 着色器在单个自定义几何网格面上无法正确显示

javascript - 动态传递父对象作为 JavaScript 中的参数