Node.js 模块特定的静态资源
是否有一种优雅的方法将静态客户端文件资源(脚本、图像等)捆绑到 Express 模块中并系统地避免命名冲突?注册静态对象的特定于模块的实例很容易,如下所示:
app.use(express.static(modulePath + '/public'));
app.use(express.static(__dirname + '/public'));
但如果两个目录都包含“styles.css”文件,则模块中的文件似乎会掩盖应用程序中的文件。模块 public 中的子目录可以用来避免这个问题,但我真正想要的是一种将模块的资源映射到任意路径的方法,这样
http://localhost:3000/mymodule/styles.css => <modulePath>/public/styles.css
http://localhost:3000/styles.css => <appPath>/public/styles.css
是否已经有办法做到这一点?我已经通过编码技巧完成了它,所以我真的在寻找推荐的方法来完成它。另外,如果我错过了一些使这完全没有必要的关键概念,我也想了解这一点。
Is there an elegant way to bundle static client-side file resources (scripts,images,etc) into an Express module and systematically avoid naming conflicts? It's easy enough to register a module-specific instance of the static object like so:
app.use(express.static(modulePath + '/public'));
app.use(express.static(__dirname + '/public'));
but if both directories contain a "styles.css" file, it would seem that the one from the module will eclipse the one for the application. A subdirectory within the module's public could be used to avoid this problem, but what I really want is a way to map the module's resources to an arbitrary path such that
http://localhost:3000/mymodule/styles.css => <modulePath>/public/styles.css
http://localhost:3000/styles.css => <appPath>/public/styles.css
Is there already a way to do this? I've already done it with coding tricks, so I'm really looking for the recommended way to get it done. Also, if I'm missing some key concept that makes this totally unnecessary, I would like to learn about that too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建
connect.static
的另一个实例并在路由中使用它:You can create another instance of
connect.static
and use it within a route:像这样的东西也应该有效:
请参阅:http://groups.google。 com/group/express-js/browse_thread/thread/c653fafc35fa85ed
Something like this should also work:
See: http://groups.google.com/group/express-js/browse_thread/thread/c653fafc35fa85ed