请问 google 的 puppeteer 模块 和 node.js 自带 的 http、 https 模块 有冲突,怎么解决? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hgjian
V2EX    Node.js

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

  •  
  •   hgjian 2019-07-09 14:14:56 +08:00 4526 次点击
    这是一个创建于 2360 天前的主题,其中的信息可能已经有所发展或是发生改变。
    测试代码段如下:

    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);
    ........});
    运行就不会报错。

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

    另外发帖用 md 就行,不用手动排版,等下班再看下是为啥。
    hgjian
        2
    hgjian  
    OP
       2019-07-09 14:49:33 +08:00 via Android
    @azh7138m 谢谢,我是从 15.3 升级到 16 的。
    azh7138m
        3
    azh7138m  
       2019-07-09 14:56:51 +08:00
    @hgjian 你用 12.6.0 会有这个问题吗?
    hgjian
        4
    hgjian  
    OP
       2019-07-09 14:57:52 +08:00 via Android
    @azh7138m 没有试,晚上试看看
    SakuraKuma
        5
    SakuraKuma  
       2019-07-09 18:32:24 +08:00 via Android
    是 agent base 改变了 http 的行为导致的 …后来 node 修复了
    hgjian
        6
    hgjian  
    OP
       2019-07-09 20:57:36 +08:00
    @SakuraKuma 谢谢解答啊
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5228 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 01:20 PVG 09:20 LAX 17:20 JFK 20:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86