LoginSignup
3
1

More than 1 year has passed since last update.

Node.js の http クライアントの使い方 (Get)

Last updated at Posted at 2018-05-04
http_get.js
// ---------------------------------------------------------------
//	http_get.js
//
//					Apr/08/2023
//
// ---------------------------------------------------------------
import https from "https"

const url = "https://httpbin.org/get" 

const options = {
  method: 'GET'
}

const req = https.request(url,options, res => {
	console.log(`statusCode: ${res.statusCode}`)

	res.on('data', d => {
	process.stdout.write(d)
	})
})

req.on('error', error => {
	console.error(error)
})

req.end()

// ---------------------------------------------------------------
package.json
{
  "type": "module"
}

実行結果

$ node ./http_get.js 
statusCode: 200
{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "X-Amzn-Trace-Id": "Root=1-621b0fb0-6e50432b6633e6453e34e664"
  }, 
  "origin": "219.126.139.79", 
  "url": "https://httpbin.org/get"
}

次のバージョンで確認しました。

$ node --version
v19.8.1

Ubuntu 系で最新の node.js をインストールする手順

sudo apt install nodejs npm
sudo npm install -g n
n ls-remote --all | less
sudo n 17.7.2
sudo apt purge nodejs npm
sudo apt autoremove
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1