Node.js 模块特定的静态资源

发布于 2024-12-09 16:04:01 字数 590 浏览 0 评论 0原文

是否有一种优雅的方法将静态客户端文件资源(脚本、图像等)捆绑到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-12-16 16:04:01

您可以创建 connect.static 的另一个实例并在路由中使用它:

app = express.createServer()

app.configure(function(){
    app.use(express.static(__dirname+'/public'))
})

// new static middleware
var myModuleStatic = express.static(__dirname+'/mymodule')

// catch all sub-directory requests
app.get('/mymodule/*', function(req, res){
    // remove subdir from url (static serves from root)
    req.url = req.url.replace(/^\/mymodule/, '')
    // call the actual middleware, catch pass-through (not found)
    myModuleStatic(req, res, function(){
        res.send(404)
    })
})

app.listen(5555)

You can create another instance of connect.static and use it within a route:

app = express.createServer()

app.configure(function(){
    app.use(express.static(__dirname+'/public'))
})

// new static middleware
var myModuleStatic = express.static(__dirname+'/mymodule')

// catch all sub-directory requests
app.get('/mymodule/*', function(req, res){
    // remove subdir from url (static serves from root)
    req.url = req.url.replace(/^\/mymodule/, '')
    // call the actual middleware, catch pass-through (not found)
    myModuleStatic(req, res, function(){
        res.send(404)
    })
})

app.listen(5555)
伴随着你 2024-12-16 16:04:01

像这样的东西也应该有效:

app.use(express.static(__dirname + '/public'));    
app.use('/mymodule', express.static(modulePath + '/public'));

请参阅:http://groups.google。 com/group/express-js/browse_thread/thread/c653fafc35fa85ed

Something like this should also work:

app.use(express.static(__dirname + '/public'));    
app.use('/mymodule', express.static(modulePath + '/public'));

See: http://groups.google.com/group/express-js/browse_thread/thread/c653fafc35fa85ed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文