gpt4 book ai didi

node.js - nodejs 代理没有响应

转载 作者:搜寻专家 更新时间:2023-10-31 23:44:02 25 4
gpt4 key购买 nike

我有一个提供静态文件(html、js、css)的 node.js 应用程序。在静态文件中,完成了一些 ajax 请求(请求中带有/TEST)。我需要将这些请求代理到在 localhost:213445 上运行的另一台服务器。静态页面正确显示,但当涉及到代理请求时,它永远挂起......

我的代码是:

var express = require("express");
var httpProxy = require('http-proxy');
var fs = require('fs');

// Create https server
var app = express.createServer({
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
});

// Handle static files
app.use(express.static(__dirname + '/public'));

// Proxy request
app.all('/TEST/*',function(req, res){
// Remove '/TEST' part from query string
req.url = '/' + req.url.split('/').slice(2).join('/');

// Create proxy
var proxy = new httpProxy.HttpProxy();
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 21345,
enableXForwarded: false,
buffer: proxy.buffer(req)
});
});

// Run application
app.listen(10443);

最佳答案

我也有同样的问题。如果你注释掉

// Handle static files
app.use(express.static(__dirname + '/public'));

您将看到代理工作。

不确定如果使用 express.static 如何修复它

Marak Squires 回复了错误 report !

This is an express problem. The Connect / Express middlewares are doing non-standard things to your response object, breaks streaming. Try using the raw request module instead of http-proxy. If you need proper middleware stack, check out https://github.com/flatiron/union

关于node.js - nodejs 代理没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7561589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com