0

I am moving a PHP application that uses Apache to a Node.js application. The PHP version uses tileservices.conf, an Apache configuration file. I need to move the configuartion settings from that file to the Node.js version of the application. Part of that configuration file is the following:

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

How would I move this into a Node.js application? I'm using Express, so can I do something along the lines of:

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

but I can't quite figure out how to go about it.

Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86
  • you can `str.match()` all those parens against the incoming url to extract the terms by position ("$1, $2, etc"). i don't know if express feeds you the path pattern though, so you might have to make a helper that loops through them all and subscribes them as-needed, using closure to pass the matching segments to the response handler. – dandavis Apr 21 '16 at 18:03
  • @dandavis I think I'm getting the gist of what you're saying but can you give an example? – Sara Fuerst Apr 21 '16 at 18:04
  • Don't you have something in front of your node.js application, like nginx or apache? doing the rewrite there would probably be more appropriate. – Kevin B Apr 21 '16 at 18:08
  • you need to accomplish `newPath=url.replace(/^\/oms\/1\.0\.0\/(.+)\/(.+)\/(.+)\/(.+)\\.[a-zA-Z]*$/g, "/sandbox/TileServices/www/MosaicRequestHandler.php?z=$2&c=$3&r=$4&library=$1&filestyle=strip")` several times by feeding it a list. imho... something like `rx=/.../; if(url.test(rx)){ url=url.replace(rx, strRep); goto(url);}` – dandavis Apr 21 '16 at 18:08
  • @KevinB I'm pretty new to Node (and Apache for that matter). Basically, I created a node project, set it up to use Express, and simply start the server on port 3000 by running `node app.js`, so I don't think so, unless something like nginx or apache is built into node – Sara Fuerst Apr 21 '16 at 18:10
  • Yeah, usually you would put nginx or apache infront of node, and reverse proxy to node so that you can have multiple applications/services all running on port 80. apache and nginx also handles the serving of static content better/easier than node. It's also easier to do url rewriting with nginx or apache. I prefer nginx when working with node, but am currently stuck with using IIS to do the same O.o – Kevin B Apr 21 '16 at 18:11
  • @KevinB Nginx is pretty similar to Express, right? – Sara Fuerst Apr 21 '16 at 18:15
  • 1
    No, not at all. it has no relation to node.js. Express is just a javascript library that makes using node's http functionality easier. nginx fulfills the same role as apache and iis in that they route incoming requests to applications. – Kevin B Apr 21 '16 at 18:15
  • @dandavis, would that handle replacing the $ variables with their proper value though? – Sara Fuerst Apr 21 '16 at 18:21

1 Answers1

0

You could use some middleware that redirects to the appropriate endpoint:

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

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

  // etc.
});

Basically, you'll just make the app use this "middleware" for every route, and you can test the incoming URL and redirect it to the correct one.

If you're going to use this middleware, make sure you invoke it before your route definitions.

(FYI, getting the full URL was taken from this answer).

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • Why would you test against the fullUrl? – Sara Fuerst Apr 21 '16 at 18:32
  • @tibsar, you don't need to necessarily use the `fullUrl`. I was just showing the `fullUrl` as an example, just in case it would need to be used. You can use just the smaller bits of it if that's all that's required for your routes. – Josh Beam Apr 21 '16 at 18:39
  • Ah okay, and would you have to use middleware to implement this? Can you just use a get with regex? – Sara Fuerst Apr 21 '16 at 18:41
  • @tibsar middleware isn't required, it's just one solution. I favor it for readability and flexibility, however some other people might recommend the "route regex" approach to avoid redirects. Whatever solution works better for your use case is what you should go with. – Josh Beam Apr 21 '16 at 18:48