![](/img/trans.png)
[英]useing mikeal's request when piping something,how to listen events to show progress with a progress-bar
[英]Mikeal's NodeJS Request modify body before piping
我正在使用mikeal為NodeJS提供的非常棒的請求模塊。 我也在快遞中使用它,我代表調用API來解決舊瀏覽器的CORS問題:
app.use(function(request, response, next) {
var matches = request.url.match(/^\/API_ENDPOINT\/(.*)/),
method = request.method.toLowerCase(),
url;
if (matches) {
url = 'http://myapi.com' + matches[0];
return request.pipe(req[method](url)).pipe(response);
} else {
next();
}
});
有沒有辦法在我將request
的響應傳回express
之前修改主體?
基於這個答案: 在node.js輸出之前更改響應體我做了一個我在自己的應用程序上使用的工作示例:
app.get("/example", function (req, resp) {
var write = concat(function(response) {
// Here you can modify the body
// As an example I am replacing . with spaces
if (response != undefined) {
response = response.toString().replace(/\./g, " ");
}
resp.end(response);
});
request.get(requestUrl)
.on('response',
function (response) {
//Here you can modify the headers
resp.writeHead(response.statusCode, response.headers);
}
).pipe(write);
});
任何改進都將受到歡迎,希望它有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.