LoginSignup
3
1

More than 1 year has passed since last update.

Docker + Next.jsでAPIを叩く時のプチハマり

Last updated at Posted at 2022-01-31

以前構築した Docker + Nginx + Node.js環境でAPIを叩く時にプチハマりしたことを書きます。
環境は↓

やりたいこと

getServerSideProps内とNextPage内それぞれでAPIを叩きたい。

やったこと

NextPage内

エンドポイントがlocalhost:8080/api/get-hogeであるとする。

const res = await fetch('http://localhost:8080/api/hoge',
{
  method: 'GET',
  // 略
});

こちらはうまくいきました。が、

getServerSideProps内

const res = await fetch('http://localhost:8080/api/hoge',
{
  method: 'GET',
  // 略
});

こっちは
Server Error / Error: connect ECONNREFFUSED ~~って出ちゃいました
調べていると、クライアントサイドとサーバーサイドで叩くドメインを変えないいけないようなので、

const res = await fetch('http://host.docker.internal:8080/api/hoge',
{
  method: 'GET',
  // 略
});

これでできました。

参考

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