如何用node.js和express来实现虚拟目录? [英] How can I implement virtual directories with node.js and express?

查看:1449
本文介绍了如何用node.js和express来实现虚拟目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  app.configure(function(){ 
app.use(express.static(__ dirname +'/ public'));
});

但是我想映射网址 http:// myhost / url1 到目录 C:\dirA\dirB http:// myhost / url2 到目录 C:\dirC



如何使用express.static来实现?

解决方案

根据您计划以这种方式映射的目录数量,您可以简单地为中的目录创建符号链接公开文件夹。



在Windows中:

  mklink / D c:\dirA \dirB public\url1 

在Linux或OSX中:

  ln -s / dirA / dirB public / url1 

那么您的静态资产服务器应该从这些目录中透明地提供(我从来没有在Windows上测试过,但我不明白为什么它不起作用)。另外,如果你想要添加一些动态路由,你可以编写自己的中间件来代替 express.static 实际上是 connect.static 。请查看连接源,看看它是如何实现的,编写自己的变体应该是相当简单的。


I want to use the express static server configuration for implementing an static server:

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

But I want to map the URL http://myhost/url1 to directory C:\dirA\dirB and http://myhost/url2 to directory C:\dirC

How can it be implemented using express.static?

解决方案

Depending on how many directories you're planning to map this way, you could simply make symlinks for those directories in your public folder.

In Windows:

mklink /D c:\dirA\dirB public\url1

In Linux or OSX:

ln -s /dirA/dirB public/url1

Then your static assets server should serve from those directories transparently (I've never tested on Windows but I don't see why it wouldn't work).

Alternatively, if you wanted to include some kind of dynamic routing, you could write your own middleware to replace express.static which is actually connect.static under the hood. Take a look at static.js in the connect source and see how it's implemented, it should be fairly straightforward to write your own variation.

这篇关于如何用node.js和express来实现虚拟目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆