在node.js中输出之前更改响应正文
Change response body before outputting in node.js
我是Node.js的新手。我正在尝试构建一个小型服务器,作为对开放数据服务POST调用的代理,然后做一些事情,绑定到表示层,最后输出到浏览器。
这是代码:
dispatcher.onGet("/metro", function(req, res) {
var r = request({body: '<?xml version="1.0" encoding="ISO-8859-1" ?><poirequest><poi_id>87087</poi_id><lng>0</lng></poirequest>'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('Public transformation public API called');
}
}).pipe(res);
res.on('finish', function() {
console.log('Request completed;');
});
});
http.createServer(function (req, res) {
dispatcher.dispatch(req, res);
}).listen(1337, '0.0.0.0');
console.log('Server is listening');
调度员是我在mpm上发现的最简单的:https://npmjs.org/package/httpdispatcher问题是:在输出到输出管道之前,我如何更改(基本上是html代码剥离)响应主体?
您可以使用类似concat流的东西来累积所有流数据,然后将其传递给回调,在将其返回到浏览器之前,您可以在回调中对其进行操作。
var concat = require('concat-stream');
dispatcher.onGet("/metro", function(req, res) {
write = concat(function(completeResponse) {
// here is where you can modify the resulting response before passing it back to the client.
var finalResponse = modifyResponse(completeResponse);
res.end(finalResponse);
});
request('http://someservice').pipe(write);
});
http.createServer(dispatcher.dispatch).listen(1337, '0.0.0.0');
console.log('Server is listening');
相关文章:
- 如何将getJson的响应保存在全局变量中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 如何将响应正文显示为html
- NodeJS-“;这种类型的响应不能有正文&”;
- 从保管箱响应正文中提取网址
- 节点.js请求对象 - 返回响应正文以供进一步操作
- 如何在 NodeJS 中传递响应参数,包括来自 POST http.request 的正文
- Spring ResponseEntity & AJAX error function:无法访问响应正文内容
- 如何在 node.js 中将 HTTP 响应正文编码为 UTF-8
- 如何读取空响应正文的 ajax post 调用的响应标头
- 获取 https 响应的正文
- Chrome扩展-如何获取HTTP响应正文
- 使用SlimerJS获取AJAX请求的响应正文
- node.js request.get以html正文响应,忽略404状态
- 在node.js中输出之前更改响应正文
- 如何处理未定义正文的响应
- JSON响应,包含http、头、元和正文
- 获取AJAX响应的正文标记
- 请求响应后正文中出现奇怪字符
- 在 Express.js 中解析 JSON 响应正文