如何写一个节点前preSS的应用程序,提供大多数地方的文件,但一些重新路由到其他域?节点、路由、应用程序、如何写

由网友(←龙霸天下`)分享简介:我的工作,通常是建立一个相对复杂的过程,然后部署到WebLogic小的web应用程序。I'm working on a small webapp that normally is built with a relatively complex process and then deployed to WebLogic...

我的工作,通常是建立一个相对复杂的过程,然后部署到WebLogic小的web应用程序。

I'm working on a small webapp that normally is built with a relatively complex process and then deployed to WebLogic.

不过,我工作的一部分是用AngularJS,而且是所有的HTML和Javascript。它通常使Ajax调用到同一域中的其他Web应用程序。为了缩短开发周期,我想避免的构建过程,只是刷新浏览器页面。

However, the portion I'm working on is using AngularJS, and is all HTML and Javascript. It normally makes ajax calls into another webapp on the same domain. To shorten my development cycle, I'd like to avoid a build process and just reload the browser page.

我想我可以用节点前preSS做到这一点,但细节逃避我。我已经成功地定义了一个非常简单的应用程序,只供应本地文件,但现在我必须弄清楚如何检测其中的一些路径作为匹配的前pression,并重新路由这些请求的请求外部域

I think I can do this with "node express", but the details escape me. I've managed to define a very simple app that just serves local files, but now I have to figure out how to detect some of those paths as matching an expression, and reroute those requests to a request to an external domain.

所以,如果它得到了/diag/stuff.html,/foo/thing.html,或只是/index.htm」明明的请求时,它会发送回匹配相同的路径的文件。但是,如果路径/fooService/.*匹配,那我也回送从获取到相同的路径的反应,但不同的主机和端口上。

So, if it gets a request for "/diag/stuff.html", "/foo/thing.html", or just "/index.html", it will send back the file matching the same path. However, if the path matches "/fooService/.*", then I have to send back the response from a GET to the same path, but on a different host and port.

这是我的琐碎应用至今:

This is my trivial app so far:

var express = require('express');
var app = express();

app.use("/", express.static(__dirname));

app.listen(8000);

更新:

我喜欢这个想法代理,所以我做了本地安装的HTTP代理(我忘了和第一次没有一个全球性的安装),然后改剧本这样:

I like the proxy idea, so I did a local install of "http-proxy" (I forgot and first did a global install) then changed the script to this:

var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();

app.use("/", express.static(__dirname));

app.get('/FooService/*', function(req, res) {
    "use strict";
    return proxy.proxyRequest(req, res, {
    host: "foohost.net",
    port: 80
    });
});

app.listen(8000);

这失败:

<path>server.js:4
var proxy = new httpProxy.RoutingProxy();
        ^
TypeError: undefined is not a function
at Object.<anonymous> (<path>server.js:4:13)

什么可能是错在这里?

What might be wrong here?

更新:

难道是有用看到的内容的console.log(HTTPPROXY)之后的规定:

Would it be useful to see the contents of "console.log(httpProxy)" after that "require"?:

function ProxyServer(options) {
  EE3.call(this);

  this.web = this.proxyRequest           = createRightProxy('web')(options);
  this.ws  = this.proxyWebsocketRequest  = createRightProxy('ws')(options);
  this.options = options;

  this.webPasses = Object.keys(web).map(function(pass) {
return web[pass];
  });

  this.wsPasses = Object.keys(ws).map(function(pass) {
return ws[pass];
  });

  this.on('error', this.onError.bind(this));

}

这是否为原因的线索新httpProxy.RoutingProxy()说:它是不确定的?

Does that provide a clue for why "new httpProxy.RoutingProxy()" says it's undefined?

推荐答案

您可以使用 HTTP代理转发请求到不同的主机。要安装 HTTP代理你需要运行 sudo的NPM安装HTTP代理。 code将处理代理将看起来像:

You can use http-proxy and forward requests to different host. To install http-proxy you need to run sudo npm install http-proxy. Code that will handle proxy will look like that:

httpProxy = require('http-proxy');
proxy = new httpProxy.RoutingProxy();
(...)
app.get('/fooService/*', function (request, response) {
    "use strict";
    return proxy.proxyRequest(request, response, {
        host : externalHost,
        port : 80
    });
});

更新

以上code正在为 HTTP代理〜0.10.x。从那时起,很多事情在图书馆发生了变化。下面你可以找到例如新版本(在写本新闻时〜1.0.2):

Above code is working for http-proxy ~0.10.x. Since then lot of things had changed in library. Below you can find example for new version (at time of writing ~1.0.2):

var httpProxy = require('http-proxy'),
    proxy = httpProxy.createProxyServer({});
(...)
app.get('/fooService/*', function (request, response) {
    "use strict";
    return proxy.web(request, response, {
        target: 'http://fooservice.com'
    });
});
阅读全文

相关推荐

最新文章