简体   繁体   中英

node express.js altering path for files served

I'm currently migrating my application to use express instead of node-static .

The functionality Im trying to recreate in express is as follows:

If a file with a certain path/filename is served, serve a different file.

The way I was doing with node-static (roughly):

var file = new require('node-static').Server('some/dir')
...
if(testRequestPath(req.url)) {
    file.serveFile(alterPath(req.url));
}

At the moment I have the files served fine using their actual filenames:

require('express')().use('/some/dir' , express.static('someOtherDir'))

Essentially what I'm looking for is how write capture the request event and alter the path using express.

Ended up being quite straightforward:

require('express')().use('/some/dir' , function(req,res){
    if(testRequestPath(req.url)) 
        res.sendFile(alterPath(req.url));
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM