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

查看:32
本文介绍了你如何在 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天全站免登陆