如何使用node.js实现虚拟目录并表达?

时间:2021-07-04 23:13:21

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

我想使用express静态服务器配置来实现静态服务器:

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

但我想将URL http:// myhost / url1映射到目录C:\ dirA \ dirB,将http:// myhost / url2映射到目录C:\ dirC

How can it be implemented using express.static?

如何使用express.static实现?

2 个解决方案

#1


7  

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:

在Windows中:

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

In Linux or OSX:

在Linux或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).

那么你的静态资产服务器应该透明地从这些目录中提供服务(我从来没有在Windows上测试,但我不明白为什么它不起作用)。

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.

或者,如果你想要包含某种动态路由,你可以编写自己的中间件来替换express.static,它实际上是connect.static。看一下connect source中的static.js,看看它是如何实现的,编写自己的变体应该相当简单。

#2


8  

This should work for you:

这应该适合你:

var serveStatic = require( "serve-static" );
app.use('/url1', serveStatic('c:\\dirA\\dirB'));
app.use('/url2', serveStatic('C:\\dirC'));

Take a look at the documentation for app.use().

看一下app.use()的文档。

#1


7  

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:

在Windows中:

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

In Linux or OSX:

在Linux或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).

那么你的静态资产服务器应该透明地从这些目录中提供服务(我从来没有在Windows上测试,但我不明白为什么它不起作用)。

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.

或者,如果你想要包含某种动态路由,你可以编写自己的中间件来替换express.static,它实际上是connect.static。看一下connect source中的static.js,看看它是如何实现的,编写自己的变体应该相当简单。

#2


8  

This should work for you:

这应该适合你:

var serveStatic = require( "serve-static" );
app.use('/url1', serveStatic('c:\\dirA\\dirB'));
app.use('/url2', serveStatic('C:\\dirC'));

Take a look at the documentation for app.use().

看一下app.use()的文档。