LoginSignup
2
1

More than 3 years have passed since last update.

Nuxt.js で他の端末から確認する時にローカルの API サーバの IP アドレスに自動でアクセスする方法

Last updated at Posted at 2019-08-26

ローカルで API のモックサーバやプロキシサーバを立ち上げることがあります。
そのうえで、Nuxt.js の dev サーバを他の端末で確認したいこともあります。
その場合、API URL を dev サーバを起動しているプライベート IP アドレスにする必要がありますが、それを手動で切り替えのは面倒なので、自動で切り替える設定になります。

nuxt.config.js
import internalIp from 'internal-ip'

export default async () => {
  let apiBaseUrl = 'http://localhost:8000/api/v1'

  if (process.env.NODE_ENV === 'develop') {
    const ip = await internalIp.v4()
    apiBaseUrl.replace(/localhost|127\.0\.0\.1|0\.0\.0\.0/, ip)
  }

  const nuxtConfig = {
    axios: {
      baseURL: apiBaseUrl
    }
  }

  return nuxtConfig
}

ちなみに、僕は環境設定ファイルに API のベース URL を記載しているので、それを上書きする形で使っています。

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