Node.jsのhttp moduleはhttpリクエストしかできず、https moduleはhttpsリクエストしかできない
タイトルがすべてシリーズ
Node.jsには標準添付ライブラリとしてhttp moduleとhttps moduleがあるが、それぞれhttp/httpsリクエストしかできないようになっている
https://nodejs.org/api/http.html
https://nodejs.org/api/https.html
http moduleでhttpsリクエストしようとしたり、https moduleでhttpリクエストしようとするとTypeErrorが発生する
code:node
❯ node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
http.get('https://example.com/')
Uncaught:
TypeError ERR_INVALID_PROTOCOL: Protocol "https:" not supported. Expected "http:"
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at new ClientRequest (node:_http_client:188:11)
at request (node:http:100:10)
at Object.get (node:http:111:15) {
code: 'ERR_INVALID_PROTOCOL'
}
https.get('http://example.com/')
Uncaught:
TypeError ERR_INVALID_PROTOCOL: Protocol "http:" not supported. Expected "https:"
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at new ClientRequest (node:_http_client:188:11)
at request (node:https:358:10)
at Object.get (node:https:392:15) {
code: 'ERR_INVALID_PROTOCOL'
}
お前まじかって気持ちになった…