简体   繁体   English

是否可以通过Apache从Node.js链接外部JS,使我能够“绕过” Apache代理使用SSE?

[英]Will linking external JS from Node.js via Apache allow me to use SSE “bypassing” the Apache proxy?

I want to serve (mostly) static content with Apache as that's what I'm comfortable in, but I want Node.js to handle server sent events. 我想用Apache提供(主要是)静态内容,因为这很适合我,但是我希望Node.js处理服务器发送的事件。 However, they are both on the same machine. 但是,它们都在同一台计算机上。

The problem is, if I set up my sseListener.html in my Apache server like so: 问题是,如果我像这样在Apache服务器中设置sseListener.html:

sseListener.html (Apache) sseListener.html(Apache)

<body>
    <div id="test"></div>

    <script type="text/javascript">
        var source = new EventSource("http://localhost:8888/test2js.js");
        var test = document.getElementById("test");

        source.addEventListener("message", function(e){
            test.innerHTML = "";
            test.innerHTML = JSON.parse(e.data).test;
        }, false);

        source.onopen = function(){
            console.log("open: ");
        }

        source.onclose = function(){
            console.log("close: ");
        }

        source.onerror = function(){
            console.log("error: ");
        }
    </script>
</body>

I get this error in the console: 我在控制台中收到此错误:

EventSource cannot load http://localhost:8888/test2js.js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.    

The following is my Node server which the above script is trying to communicate with: 以下是上述脚本尝试与之通信的我的节点服务器:

test2js.js (Node) test2js.js(节点)

var http = require("http");
var date = new Date();

  function onRequest(request, response) {
    console.log("Request received.");
    response.writeHead(200, {"Content-Type": "text/event-stream"});
    response.write("{ \"id\": \"" + date + "\", \"data\": \"test\"}");
    response.end();
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");

I realize this is because I'm trying to use an EventSource client served via Apache to communicate with a server hosted by Node, just like how an Ajax call would fail due to cross-domain issues. 我意识到这是因为我正在尝试使用通过Apache服务的EventSource客户端与Node托管的服务器进行通信,就像Ajax调用由于跨域问题而失败一样。

Reading around the internet, I know I can just set up a proxy from Apache to Node, but then I've also read that this defeats my purpose of having Node handle concurrent connections - Apache will set up threads for its communication with Node instead of not having to do that at all. 在互联网上阅读,我知道我可以设置从Apache到Node的代理,但是我还读到,这违背了让Node处理并发连接的目的-Apache将设置线程以与Node进行通信,而不是完全不必这样做。

How I understand this process to work: 我如何理解此过程的工作原理:

在此处输入图片说明

But, I don't like that the requests have to route to Apache first then go to Node second. 但是,我不喜欢请求必须先路由到Apache,然后再路由到Node。 I want the requests to go straight to Node. 我希望请求直接进入Node。

I realize there are two "obvious" ways to do this: 我意识到有两种“显而易见”的方法可以做到这一点:

  1. Set up my whole app in Node -> This is not a good option for me as I am much more comfortable with PHP and my JavaScript ability is not as good as my PHP 在Node上设置我的整个应用程序->这对我来说不是一个好选择,因为我对PHP更加满意并且我的JavaScript能力不如我的PHP
  2. Just handle the SSE by Apache -> I'd rather not do this either. 只需通过Apache处理SSE->我也不想这样做。 The server I'm running on isn't actually mine alone, I'm only mounting my app on it and "borrowing" the space so I'd like to implement something as lightweight as I can muster. 我运行的服务器实际上并不是我自己的服务器,我只是在上面安装我的应用程序并“借用”空间,所以我想实现一个轻量级的功能。

So thinking about the problem, I came up with a solution I'm not sure will work, but I don't know how to implement it either: make Apache fetch the client js code and link it to sseListener.html, like so: 因此,考虑到这个问题,我想出了一个不确定的解决方案,但我也不知道如何实现它:使Apache提取客户端js代码并将其链接到sseListener.html,如下所示:

<body>
    <div id="test"></div>
    <script type="text/javascript" src="path/to/node/file/system/sseClient.js"></script>
</body>

The way I'm guessing this would work, then, would be like: 那么,我猜测这将起作用的方式将是: 在此处输入图片说明

In other words: save the client.js file (with EventSource) in the Node directory, grab that file with Apache and attach it somehow, serve that to the client. 换句话说:将client.js文件(带有EventSource)保存在Node目录中,使用Apache抓取该文件并将其以某种方式附加到客户端上。 So when the client makes a request, it will go straight to Node rather than Apache. 因此,当客户端发出请求时,它将直接到达Node而不是Apache。

So my questions: 所以我的问题是:

  1. Is this possible? 这可能吗?
  2. If so, how can I implement this? 如果是这样,我该如何实施?
  3. Otherwise, are there any other methods to serve files to clients by Apache but offset the SSE side to Node, without making it so that Apache has to also handle the connections? 否则,是否还有其他方法可以通过Apache将文件提供给客户端,但是将SSE端偏移到Node, 而无需这样做,以便Apache也必须处理连接?

Literally, all I want my Node server to do is push data to clients that Apache will generate. 从字面上看,我希望我的Node服务器要做的就是将数据推送到Apache将生成的客户端。 It won't do anything complex at all. 它不会做任何复杂的事情。 I just want to use its ability for concurrent connections to make a more efficient app. 我只想将其功能用于并发连接以使应用程序更高效。

I think proxy is bad idea for your aims. 我认为代理对于您的目标不是一个好主意。 Try this: 尝试这个:

    var source = new EventSource("http://localhost:8888/test2js.js");
    var test = document.getElementById("test");

    source.onmessage = function(e){
        test.innerHTML = "";
        var data = JSON.parse(e.data);
        test.innerHTML = data.test + data.date;
    };

In your test2js.js : 在您的test2js.js中:

  function onRequest(request, response) {
  console.log("Request received.");
  response.writeHead(200, {"Content-Type": "text/event-stream",
                           "Cache-Control": "no-cache",
                            "Access-Control-Allow-Origin": "*"
                     });
  response.write("data:{\"date\":"+date+",\"test\":\"some value\"}\n\n");
  response.end();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM