3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Browser/Node.js両対応、シンプルなHTTPクライアント"bent"

Posted at

Node.jsでHTTPリクエストしたいなーと思って一番メジャーそうな request - npm を覗いたら deprecated になってるじゃないですか!

代わりに、シンプルなHTTPクライアントで良いの無いかなーと調べてたら、requestのissuesコメントにあったbentというクライアントに辿り着きました。

bent

使い方はとても簡単。

localhost:3000GETしてレスポンスボディを文字列で受けたい場合...

const bent = require("bent");

const httpGet = bent("http://localhost:3000", "GET", "string");

const responseBody = httpGet("/");

これだけ。

Node.jsではhttpを、Browserではfetchというように内部で使い分けてるので、コードを統一しつつNode.jsで実行時にはCORS問題が発生しません。

3
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?