如何在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?