我正在开发node.js代理服务器应用程序,我希望它支持HTTP和HTTPS(SSL)协议(作为服务器).
我目前正在使用node-http-proxy这样的:
const httpProxy = require('http-proxy'),
http = require('http');
var server = httpProxy.createServer(9000, 'localhost', function(req, res, proxy) {
console.log(req.url);
proxy.proxyRequest(req, res);
});
http.createServer(function(req, res) {
res.end('hello!');
}).listen(9000);
server.listen(8000);
Run Code Online (Sandbox Code Playgroud)
我设置我的浏览器使用HTTP代理localhost:8000,它的工作原理.我还想捕获HTTPS请求(即设置我的浏览器以localhost:8000用作HTTPS代理并在我的应用程序中捕获请求).你能帮帮我怎么办?
PS:
如果我订阅upgrade了httpProxy服务器对象的事件,我可以获得请求,但我不知道如何转发请求并向客户端发送响应:
server.on('upgrade', function(req, socket, head) {
console.log(req.url);
// I don't know how to forward the request and send the response to client
});
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
正如标题所述,我现在正在接收节点.我看了一下Express,主要是通过观看expressjs.com上的视频,给人留下了深刻的印象.
但是,它让我想起了很多Rails.我总觉得我从未真正理解当我使用RoR时发生的事情.它结合了如此多的魔力,它真的让我不自信并把我推开了.我不需要了解裸机,但我喜欢很好地了解事情的方式/原因.
可能只是我对Jade和Sass的无知使Express视频看起来像巫术.
可能是相关的:我已经使用JavaScript和一些库(jQuery,Dojo,Raphaeljs,ESRI)已经有几年了,所以我不是完全没有想法的.
这可能是重复的,但我还没有找到专门与我的问题相关的线程。
我正在进行以下 API 调用:
const config = {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS"
}
};
const {
data: { ip }
} = await axios.get("https://api.ipify.org?format=json", config);
Run Code Online (Sandbox Code Playgroud)
这会引发错误:
Access to XMLHttpRequest at 'https://api.ipify.org/?format=json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我将我的应用程序部署到 Heroku 时,API 调用按预期工作。但是在我的本地机器上开发时它不起作用。不确定我在这里缺少什么。
当我尝试在angular 7 Web应用程序中执行PATCH请求时遇到问题。在我的后端,我有:
app.use((req, res, next) => {
res.set({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "'Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token'",
});
next();
});
Run Code Online (Sandbox Code Playgroud)
在前端服务中,我已经:
patchEntity(ent: any, id) {
let headers = new Headers({ 'Content-Type': '*' });
let options = new RequestOptions({ headers: headers });
return this.http.patch('my_url', ent).map((res: Response) => res.json());
};
Run Code Online (Sandbox Code Playgroud)
错误是:
Access to XMLHttpRequest at 'my_url' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. …Run Code Online (Sandbox Code Playgroud) 我需要能够通过我的节点服务器提供副本站点(到www.google.com,www.facebook.com等任何站点).我找到了这个库:
https://github.com/nodejitsu/node-http-proxy
代理请求时我使用了以下代码:
options = {
ignorePath: true,
changeOrigin: false
}
var proxy = httpProxy.createProxyServer({options});
router.get(function(req, res) {
proxy.web(req, res, { target: req.body.url });
});
Run Code Online (Sandbox Code Playgroud)
但是,此配置会导致大多数站点出错.根据网站的不同,我会收到Unknown service来自目标网址的错误,或者来自Invalid host这些行的错误信息.但是,当我通过
changeOrigin: true
Run Code Online (Sandbox Code Playgroud)
我得到了一个正常运行的代理服务,但我的用户浏览器被重定向到他们请求的实际网址,而不是我的(因此,如果req.body.url = http://www.google.com请求将转到http://www.google.com)
我怎样才能使我的网站的网址显示出来,但是我可以准确地复制正在显示的内容?我需要能够在请求中添加一些JS文件,我正在使用另一个库.
为了澄清,这里是问题的摘要:
用户请求具有url属性的资源
这url是以.的形式http://www.example.com
我的服务器运行www.pv.com,需要能够指导用户www.pv.com/http://www.example.com
返回的HTTP响应
www.pv.com/http://www.example.com是完整的表示
http://www.example.com.我需要能够在此响应中添加自己的Javascript/HTML文件.
我正在尝试将( How to create a simple http proxy in node.js? )上接受的答案从http转换为https。
当我尝试从浏览器访问代理时,服务器退出并抛出此错误:
events.js:171
throw TypeError('listener must be a function');
^
TypeError: listener must be a function
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
var https = require('https');
var fs = require('fs');
var ssl = {
ca: fs.readFileSync("cacert.pem"),
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem")
};
https.createServer(ssl, onRequest).listen(3000, '127.0.0.1');
function onRequest(client_req, client_res) {
console.log('serve: ' + client_req.url);
var options = {
hostname: 'www.example.com',
port: 80,
path: client_req.url,
method: 'GET'
};
var ssl = {
ca: fs.readFileSync("cacert.pem"),
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem") …Run Code Online (Sandbox Code Playgroud)