请问 google 的 puppeteer 模块 和 node.js 自带 的 http、 https 模块 有冲突,怎么解决?

2019-07-09 14:14:56 +08:00
 hgjian
测试代码段如下:

async function puppeteer_promise ( ) {
........var puppeteer = require('puppeteer'); /*** 屏蔽测试 ***/
........return ;
}

async function http_promise ( ) {
........var http = require('http');
........var https= require('https');

........var url = new URL ( "https://www.baidu.com/" ) ;
........var http = ( url.protocol == 'https:' ? https : http ) ; // 重新赋值 http ,判断是 http 还是 https ;
........console.log( "url.protocol:" + url.protocol ) ;

........http.get ( url , function ( res ) { /*** 屏蔽测试 ***/

........}).on('error', function (err) {
................console.log(err);
........});

........return ;
}

async function test_alldata ( ) {

........console.log( "开始测试!" ) ;

........await puppeteer_promise();
........console.log( "puppeteer 结束!" ) ;

........await http_promise();

........console.log( "结束测试!" ) ;

........return ; // 隐式返回 Promise.resolve( ); 函数 ;

}

test_alldata();

报错信息如下:
(node:6212) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined
at urlToOptions (internal/url.js:1259:28)
at Object.request ( https.js:279:15)
at Object.request (C:\Users\Claire\Desktop\spider\node_modules\agent-base\patch-core.js:25:22)
at Object.https.get (C:\Users\Claire\Desktop\spider\node_modules\agent-base\patch-core.js:48:21)
at http_promise (C:\Users\Claire\Desktop\spider\test.js:17:7)
at test_alldata (C:\Users\Claire\Desktop\spider\test.js:34:9)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:832:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:6212) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:6212) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Node 10.16
WIN10_x64 系统

屏蔽 var puppeteer = require('puppeteer');
或者 屏蔽
........http.get ( url , function ( res ) { /*** 屏蔽测试 ***/

........}).on('error', function (err) {
................console.log(err);
........});
运行就不会报错。

请问各位,知道是怎么回事吗?
有没有什么解决办法啊?
4137 次点击
所在节点    Node.js
6 条回复
azh7138m
2019-07-09 14:36:15 +08:00
有趣,node v10.15.3 也有这个问题,v12.6.0 没有这个问题。

另外发帖用 md 就行,不用手动排版,等下班再看下是为啥。
hgjian
2019-07-09 14:49:33 +08:00
@azh7138m 谢谢,我是从 15.3 升级到 16 的。
azh7138m
2019-07-09 14:56:51 +08:00
@hgjian 你用 12.6.0 会有这个问题吗?
hgjian
2019-07-09 14:57:52 +08:00
@azh7138m 没有试,晚上试看看
SakuraKuma
2019-07-09 18:32:24 +08:00
是 agent base 改变了 http 的行为导致的 …后来 node 修复了
hgjian
2019-07-09 20:57:36 +08:00
@SakuraKuma 谢谢解答啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/581319

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX