apache mod_rewrite 相当于node.js?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找一个好的 modrewrite 库。您可能想查看 connect-modrewrite
If you are looking for a good modrewrite library. You might want to look at connect-modrewrite
如果你有一个使用 NodeJS 运行的 HTTP 服务器,你就有 2 个对象:请求和响应。请求中包含请求的 url。使用 require('url') 您可以解析此请求的 url,例如获取请求的路径名。
然后你用它做什么,显然取决于你自己的代码。因此,根据 www.nodejs.org 上的默认示例,您最终会得到如下结果:
您可以使用
更多信息:
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:
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:
正如前面的答案所建议的,您需要自己编写;前面的两个答案都更倾向于专门处理不同的路径。
您可能会发现我的节点反向代理很有帮助,因为它有很多代码处理重写规则。这与前面的答案不同,因为它允许您匹配“/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.
如果您正在寻找等效的路由器(尽管在技术上不是这样,因为路由器实际上并不“重写”任何东西),那么那里有很多路由器。最值得注意的是 Connect 路由器(Express 是在其上构建的):https://github.com/senchalabs/connect
它看起来像这样:
不过,最好先尝试一下较低级别的 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:
It might be better to mess around with the lower-level http interface first though, to get a good feel for it.
有一个重写模块。当与中间件中的另一个代理模块一起使用时,它们作为反向代理一起工作。
我在本地盒子中开发单页应用程序时使用它们(因此我不必在本地配置 apache/nginx),
这是为了避免 CORS 并将所有页面(js/css/images 除外)发送到 index.html 以便 SPA工作。
检查我是否使用了
[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.
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 toapi.server.dev
, the rest goes toui.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 theconnect
before sent to their respective servers.And yes, in case of redirects,
proxy-middleware
will change theLocation
header to belocalhost:9000