简体   繁体   English

NodeJS:使用JavaScript内容编写http响应

[英]NodeJS: write http response with JavaScript content

I have a server running on port 5000 and users don't have access to this port. 我有一台运行在端口5000上的服务器,用户无权访问此端口。

I started another node server on 4000 and want to read the http content ( localhost:5000/test ) (content has javascripts) and users should see it on the browser by accessing port localhost:4000/test 我在4000上启动了另一个节点服务器,并想读取http内容( localhost:5000/test )(内容包含JavaScript),用户应该通过访问端口localhost:4000/test在浏览器上看到它

Something like http content forward not the URL forward. 诸如http内容转发而不是URL转发之类的东西。

Any examples? 有什么例子吗? Thanks in advance 提前致谢


For this problem, I would recommend using a proxy such as https://github.com/nodejitsu/node-http-proxy 对于这个问题,我建议使用代理,例如https://github.com/nodejitsu/node-http-proxy
The code that you would need to implement on your server on port 4000 should look something like this: 您需要在端口4000的服务器上实现的代码如下所示:

const http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'http://localhost:5000'}).listen(4000); 

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

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