接受来自 http.request() 的所有 options,默认值有一些不同:
protocol 默认值: 'https:'
port 默认值: 443
agent 默认值: https.globalAgent
callback
返回:
发出请求到安全的 Web 服务器。
还接受来自 tls.connect() 的以下额外的 options:ca、cert、ciphers、clientCertEngine、crl、dhparam、ecdhCurve、honorCipherOrder、key、passphrase、pfx、rejectUnauthorized、secureOptions、secureProtocol、servername、sessionIdContext、highWaterMark。
options 可以是对象、字符串或 URL 对象。 如果 options 是字符串,则会自动使用 new URL() 解析。 如果是 URL 对象,则会自动转换为普通的 options 对象。
https.request() 返回 http.ClientRequest 类的实例。 ClientRequest 实例是可写流。 如果需要使用 POST 请求上传文件,则写入 ClientRequest 对象。
export const request = async(host:string,path:string):Promise =>{
return new Promise( async (resolve) => {
const https = require('https');
const options = {
hostname: host,
port: 443,
path: path,
method: 'GET'
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
let data = "";
res.on('data', (d) => {
return data+=d;
});
res.on('end', (d) => {
let jsonData:any = {}
try{
jsonData = JSON.parse(data);
}catch (e){
return resolve({code:-1,msg:`${e}`})
}
return resolve(jsonData)
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
});
}