Apache mod_rewrite for node.js?

Is there an equivalent module for Node.js that does what Apache mod_rewrite does? or is there a standard language construct that provides equivalent functionality?

I am just starting out with Node and want to convert my server to this platform.

+7
source share
5 answers

As suggested in previous answers, you need to write this yourself; the two previous answers were more focused on handling different paths.

You may find my node reverse proxy useful, as it has a lot of code to handle rewrite rules. This is different from previous answers because it allows you to map a rule, for example, “/ people / ([az] *)” and redirect to “/cgi-bin/index.cgi?user=$1”, which is very similar to mod_rewrite.

+2
source

If you have an HTTP server working with NodeJS, you have 2 objects, a request and a response. The request contains the requested URL. Using require ('url'), you can parse this requested url and, for example, get the requested path name.

What you do with it then obviously depends on your own code. Therefore, based on the default example at www.nodejs.org, you will get something like this:

var http = require('http'); http.createServer(function (req, res) { var requestedURL = require('url').parse( req.url ); res.writeHead(200, {'Content-Type': 'text/plain'}); res.write( "You requested " + requestedURL.pathname + "\n" ); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); 

What you can check with http://127.0.0.1:1337/foo/bar . Where you can use requestedURL.pathname to determine what you want to do, ideally you would create your own - or use a third-party routing library. They are available, ExpressJS is a fairly well-known NodeJS infrastructure that can help you take care of a lot, but I have no experience with this.

Additional Information:

+3
source

If you are looking for a good modrewrite library. You can watch connect-modrewrite

+3
source

If you are looking for an equivalent (albeit not technically, because routers do not actually rewrite anything), there are several routers. First of all, the Connect router (on which Express is built): https://github.com/senchalabs/connect

It will look something like this:

 app.get('/', function(req, res) { res.end('hello, here is the home page.'); }); 

It might be better to first chat with the lower-level http interface to get a good idea about this.

0
source

There is a rewrite module. And when they are used with another proxy module in the middleware, they work together as a reverse proxy.

I use them when developing Single Pages Applications in my local field (so I don't need to configure apache / nginx locally)

This is to avoid CORS and send all pages (except js / css / images) to index.html for SPA to work.

 var connect = require('connect'); var modRewrite = require('connect-modrewrite'); var proxy = require('proxy-middleware'); var url = require('url'); var app = connect() .use(modRewrite([ "^\/api\/(.*) /send-to-api/api/$1 [L]", "^(.*)\/css\/(.*) /send-to-ui/css/$2 [L]", "^(.*)\/js\/(.*) /send-to-ui/js/$2 [L]", "^(.*)\/images\/(.*) /send-to-ui/images/$2 [L]", "^(.*)\/fonts\/(.*) /send-to-ui/fonts/$2 [L]", "^(.*) /send-to-ui/index.html [L]" ])) .use('/send-to-api', proxy(url.parse('http://api.server.dev/'))) // Don't forget the last backslash .use('/send-to-ui', proxy(url.parse('http://ui.server.dev/' ))) // Don't forget the last backslash .listen(9000) 

Make sure I use the [L] flag because I want it to rewrite and skip the rest of the rules.

In this case, only the /api tags receive the proxy server to api.server.dev , the rest - ui.server.dev . The url /send-to-api and /send-to-ui prefixes are temporary, and I use them to distinguish what comes next, it is removed by connect before being sent to the corresponding servers.

And yes, if redirected, proxy-middleware will change the Location header to localhost:9000

0
source

All Articles