How can I implement virtual directories with node.js and express?

前端 未结 2 1998
日久生厌
日久生厌 2021-01-12 04:07

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

app.configure(function() {
    app.use(express.static(__dirname + \'         


        
相关标签:
2条回答
  • 2021-01-12 04:13

    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.

    0 讨论(0)
  • 2021-01-12 04:27

    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().

    0 讨论(0)
提交回复
热议问题