Nodejs http request 返回内容
Nodejs http request 返回内容
var http = require(‘http’);
http.createServer(function(request, response) { var proxyRequest = http.request({ host: request.headers[‘host’], port: 80, path: request.url, method: request.method, headers: request.headers }, function(proxyResponse) { console.log(proxyResponse.headers); response.writeHead(proxyResponse.statusCode, proxyResponse.headers); proxyResponse.pipe(response); }); request.pipe(proxyRequest); }).listen(8080);
请问怎么修改proxyResponse的html?
要修改 proxyResponse
的 HTML 内容,我们可以在将响应内容传递给客户端之前对其进行处理。这可以通过监听 proxyResponse
的数据事件来实现,然后对数据进行操作,最后将修改后的内容发送给客户端。
以下是修改后的代码示例:
var http = require('http');
http.createServer(function (request, response) {
var proxyRequest = http.request({
host: request.headers['host'],
port: 80,
path: request.url,
method: request.method,
headers: request.headers
}, function (proxyResponse) {
// 设置响应头
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
// 初始化一个变量来存储响应体
let body = [];
// 监听 'data' 事件来收集响应体的数据片段
proxyResponse.on('data', chunk => {
body.push(chunk);
});
// 监听 'end' 事件来处理收集到的数据
proxyResponse.on('end', () => {
// 将数据片段拼接成完整的字符串
const responseBody = Buffer.concat(body).toString();
// 修改 HTML 内容
const modifiedBody = responseBody.replace(/<body>/i, '<body><h1>Hello, World!</h1>');
// 将修改后的内容写入响应流
response.end(modifiedBody);
});
});
// 将请求从客户端转发到代理服务器
request.pipe(proxyRequest);
}).listen(8080, () => {
console.log('Server running at http://127.0.0.1:8080/');
});
解释
- 创建服务器:使用
http.createServer()
创建一个 HTTP 服务器。 - 发起代理请求:根据客户端请求的
host
,port
,path
,method
和headers
创建一个代理请求。 - 设置响应头:将代理服务器的响应状态码和响应头复制到客户端响应中。
- 收集响应体:通过监听
data
事件收集代理服务器响应的每一个数据片段,并将其存储在一个数组中。 - 处理响应体:当所有数据片段都收集完毕时(即触发
end
事件),将这些片段拼接成完整的字符串。 - 修改内容:使用字符串替换方法修改 HTML 内容(例如,在
<body>
标签内添加一个标题)。 - 发送响应:将修改后的内容通过
response.end()
发送给客户端。
这样,你就可以在返回给客户端之前修改代理服务器的响应内容了。
外边新建一个处理 HTML 的函数, 在获取 proxyResponse 的函数里调用那个函数?
我也没懂你的意思… 处理 HTML 不是直接字符串处理方法去做就好了么
怎么回复的内容不见了???
要在 proxyResponse
的 HTML 上进行修改,你可以在 pipe
操作之前对数据进行处理。具体来说,你可以监听 proxyResponse
的 data
事件来收集响应内容,然后根据需要对其进行修改。最后,将修改后的内容通过 response
发送出去。
以下是一个示例代码:
var http = require('http');
http.createServer(function (request, response) {
var proxyRequest = http.request({
host: request.headers['host'],
port: 80,
path: request.url,
method: request.method,
headers: request.headers
}, function (proxyResponse) {
var chunks = [];
// 收集响应数据
proxyResponse.on('data', function (chunk) {
chunks.push(chunk);
});
// 数据收集完成后处理
proxyResponse.on('end', function () {
// 将 chunks 转换为字符串
var htmlContent = Buffer.concat(chunks).toString();
// 修改 HTML 内容(例如替换字符串)
htmlContent = htmlContent.replace('旧字符串', '新字符串');
// 设置响应头并发送修改后的数据
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
response.end(htmlContent);
});
});
request.pipe(proxyRequest);
}).listen(8080);
console.log("Server is listening on port 8080");
解释
-
收集响应数据:
- 我们使用一个数组
chunks
来存储proxyResponse
中的数据块。 - 监听
data
事件,并将每次接收到的数据块推入chunks
数组。
- 我们使用一个数组
-
处理数据:
- 当
proxyResponse
结束时(触发end
事件),我们将所有数据块拼接成一个完整的字符串htmlContent
。 - 在这之后,你可以对
htmlContent
进行任意修改(例如使用String.prototype.replace
方法)。
- 当
-
发送修改后的内容:
- 使用
response.writeHead
设置响应头。 - 使用
response.end
发送修改后的 HTML 内容。
- 使用
这样,你就可以在返回客户端之前修改从代理服务器获取的 HTML 内容了。