2016-04-21 66 views
0

我正在將使用Apache的PHP應用程序移到Node.js應用程序中。 PHP版本使用tileservices.conf,一個Apache配置文件。我需要將配置文件從該文件移動到應用程序的Node.js版本。部分配置文件如下:Apache的Rewrite規則的Node.js版本

RewriteEngine on 
RewriteRule ^/1.0.0/([a-zA-Z0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)\.[a-zA-Z]*$ /sandbox/TileServices/tiles/tc/cache_server.php?z=$2&c=$3&r=$4&cache=$1 
RewriteRule ^/nwomtest/1.0.0/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/tc/cache_server_new.php?z=$2&c=$3&r=$4&cache=$1 
RewriteRule ^/nocache/nwomtest/1.0.0/(.+)/(.+)/(.+)/(.+)$ /sandbox/TileServices/www/tc/cache_server_new.php?z=$2&c=$3&r=$4&cache=$1&do_not_cache=nocache 
RewriteRule ^/nocache/nwomtest/1.0.0/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/tc/cache_server_new.php?z=$2&c=$3&r=$4&cache=$1&do_not_cache=nocache 

RewriteRule ^/oms/1.0.0/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/MosaicRequestHandler.php?z=$2&c=$3&r=$4&library=$1&filestyle=strip 
RewriteRule ^/omb/1.0.0/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/MosaicRequestHandler.php?z=$2&c=$3&r=$4&library=$1&filestyle=block 
RewriteRule ^/ms/1.0.0/(.+)/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/Mosaic/Mosaic.php?z=$3&c=$4&r=$5&resource=$1&tkid=$2&filestyle=strip 
RewriteRule ^/mb/1.0.0/(.+)/(.+)/(.+)/(.+)/(.+)\.[a-zA-Z]*$ /sandbox/TileServices/www/Mosaic/Mosaic.php?z=$3&c=$4&r=$5&resource=$1&tkid=$2&filestyle=block 

我該如何將它移動到Node.js應用程序中?我使用的是快速,所以我可以做線沿線的東西:

app.get('regexstring', function(req, res){ 
    res.render(...) 
}); 

,但我不能完全弄清楚如何去做。

+0

你可以'str.match()'所有那些對傳入網址的parens提取位置(「$ 1,$ 2等)」的條款。我不知道express是否會爲您提供路徑模式,因此您可能必須創建一個幫助程序,通過它們遍歷它們並根據需要預訂它們,方法是使用closure將匹配的段傳遞給響應處理程序。 – dandavis

+0

@dandavis我想我明白你的意思了,但你能舉個例子嗎? –

+0

難道你在node.js應用程序的前面沒有東西,比如nginx或apache?在那裏重寫可能會更合適。 –

回答

0

你可以使用一些中間件重定向到相應的端點:

app.use('*', function(req, res, next) { 
    var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl; 

    if(someUrlRegex.test(fullUrl)) { 
    res.redirect('someOtherUrl'); 
    } 

    // etc. 
}); 

基本上,你只讓應用程序中使用這種「中間件」爲每路線,並且可以測試輸入的URL和重定向它到了正確的一個。

如果您打算使用此中間件,請確保在路由定義之前調用它。 (僅供參考,完整網址取自this answer)。

+0

爲什麼要測試fullUrl? –

+0

@tibsar,你不需要使用'fullUrl'。我只是把'fullUrl'作爲一個例子,以防萬一需要使用它。如果這是路由所需的,那麼只需使用它的較小比特。 –

+0

啊好吧,你會不得不使用中間件來實現這個?你能用正則表達式嗎? –