如何在Node.js中遵循HTTP重定向? [英] How do you follow an HTTP Redirect in Node.js?

查看:78
本文介绍了如何在Node.js中遵循HTTP重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在node中打开一个页面并处理我的应用程序中的内容.这样的事情似乎运作良好:

I want to open a page up in node and process the contents in my application. Something like this seems to work well:

var opts = {host: host, path:pathname, port: 80};
http.get(opts, function(res) {
  var page = '';
  res.on('data', function (chunk) {
    page += chunk;
  });
  res.on('end', function() {
     // process page
  });

但是,如果页面返回301/302重定向,则此操作无效.万一有多个重定向,我将如何以可重用的方式进行操作? http上方是否有包装器模块,可以更轻松地处理来自节点应用程序的http响应?

This doesn't work, however, if the page returns an 301/302 redirect. How would I do that in a reusable way in case there are multiple redirects? Is there a wrapper module on top of the http to more easily handle processing http responses from a node application?

推荐答案

http上方是否有包装器模块,可以更轻松地处理来自节点应用程序的http响应?

Is there a wrapper module on top of the http to more easily handle processing http responses from a node application?

request

请求中的重定向逻辑

这篇关于如何在Node.js中遵循HTTP重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆