测试代码段如下:
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);
........});
运行就不会报错。
请问各位,知道是怎么回事吗?
有没有什么解决办法啊?