问题
Is there an equivalent module for Node.js that does what Apache's mod_rewrite does? or is there a standard language construct that provides equivalent functionality?
I'm just getting started with Node and looking to convert my server to this platform.
回答1:
As suggested by the previous answers you need to write it yourself; the two previous answers were both more orientated towards handling different paths specially.
You might find my node reverse proxy helpful, as it has a lot of code for handling rewrite rules. This is different than the previous answers because it allows you to match on a rule such as "/people/([a-z]*)" and redirect to "/cgi-bin/index.cgi?user=$1" which is very similar to mod_rewrite.
回答2:
If you are looking for a good modrewrite library. You might want to look at connect-modrewrite
回答3:
If you have a HTTP server running with NodeJS you have 2 objects, request and response. The request contains the requested url. Using a require('url') you can parse this requested url and for example get the pathname that's requested.
What you then do with it, is up to your own code obviously. So based on the default example on www.nodejs.org you'd end up with 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");
Which you can test with http://127.0.0.1:1337/foo/bar. Where you can use requestedURL.pathname
to determine what you'd want to do, ideally you'd create your own - or use a 3rd party - routing library. They are available, ExpressJS is a pretty famous NodeJS framework which might help take care of a lot of things for you, but I have no experience with it myself.
More information:
- Now dead: [http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/]
- http://expressjs.com/
回答4:
If you're looking for an equivalent (although not technically, because routers don't actually "rewrite" anything), there are a number of routers out there. Most notably the Connect router (upon 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 mess around with the lower-level http interface first though, to get a good feel for it.
回答5:
There is a rewrite module. And when used with another proxy module in the middleware, they work together as a Reverse Proxy.
I use them while developing Single Page Applications in my local box (so I dont have to configure apache/nginx locally)
This in order to avoid CORS and send all pages (except js/css/images) to index.html for the 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)
Check that I use [L]
flag because I want it to rewrite and skip the rest of the rules.
In this case, only the /api
urls get proxied to api.server.dev
, the rest goes to ui.server.dev
.
The url prefixes /send-to-api
and /send-to-ui
are temporary and I use them to differentiate what will go where, it is removed by the connect
before sent to their respective servers.
And yes, in case of redirects, proxy-middleware
will change the Location
header to be localhost:9000
来源:https://stackoverflow.com/questions/5890043/apache-mod-rewrite-equivalent-for-node-js