apache mod_rewrite 相当于node.js?

发布于 2024-11-04 19:41:37 字数 113 浏览 0 评论 0原文

Node.js 是否有一个等效模块可以完成 Apache 的 mod_rewrite 的功能?或者是否存在提供等效功能的标准语言结构?

我刚刚开始使用 Node,并希望将我的服务器转换到这个平台。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执着的年纪 2024-11-11 19:41:37

如果您正在寻找一个好的 modrewrite 库。您可能想查看 connect-modrewrite

If you are looking for a good modrewrite library. You might want to look at connect-modrewrite

悟红尘 2024-11-11 19:41:37

如果你有一个使用 NodeJS 运行的 HTTP 服务器,你就有 2 个对象:请求和响应。请求中包含请求的 url。使用 require('url') 您可以解析此请求的 url,例如获取请求的路径名。

然后你用它做什么,显然取决于你自己的代码。因此,根据 www.nodejs.org 上的默认示例,您最终会得到如下结果:

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");

您可以使用

更多信息:

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:

幸福不弃 2024-11-11 19:41:37

正如前面的答案所建议的,您需要自己编写;前面的两个答案都更倾向于专门处理不同的路径。

您可能会发现我的节点反向代理很有帮助,因为它有很多代码处理重写规则。这与前面的答案不同,因为它允许您匹配“/people/([az]*)”等规则并重定向到“/cgi-bin/index.cgi?user=$1”,这非常相似到 mod_rewrite。

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.

请止步禁区 2024-11-11 19:41:37

如果您正在寻找等效的路由器(尽管在技术上不是这样,因为路由器实际上并不“重写”任何东西),那么那里有很多路由器。最值得注意的是 Connect 路由器(Express 是在其上构建的):https://github.com/senchalabs/connect

它看起来像这样:

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

不过,最好先尝试一下较低级别的 http 接口,以获得良好的感觉。

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.

沉鱼一梦 2024-11-11 19:41:37

有一个重写模块。当与中间件中的另一个代理模块一起使用时,它们作为反向代理一起工作。

我在本地盒子中开发单页应用程序时使用它们(因此我不必在本地配置 apache/nginx),

这是为了避免 CORS 并将所有页面(js/css/images 除外)发送到 index.html 以便 SPA工作。

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)

检查我是否使用了 [L] 标志,因为我希望它重写并跳过其余规则。

在这种情况下,只有 /api url 被代理到 api.server.dev,其余的都转到 ui.server.dev
url 前缀 /send-to-api/send-to-ui 是临时的,我用它们来区分什么会去哪里,它被 删除>连接之前发送到各自的服务器。

是的,在重定向的情况下,代理中间件会将 Location 标头更改为 localhost:9000

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文