簡體   English   中英

Mikeal的NodeJS請求在管道之前修改主體

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM