0
0

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 1 year has passed since last update.

NUXT.JSでHerokuにデプロイした時データが取得できない問題

Last updated at Posted at 2022-06-13

NUXTのaxios-moduleBaseURLは設定が何もないとhttp://localhost:3000になってHeroku上で上手く動いてくれなくなります。
スクリーンショット 2022-06-12 8.11.30.png
こちらの部分をhttp://XXXXXXXXXXXXXXXXXX.herokuapp.com/apiとしたい。
スクリーンショット 2022-06-13 8.32.27.png
window.location.originを付与して、GETできるように致します。 

this.$axios.$get(window.location.origin + "/api/XXXX");

スクリーンショット 2022-06-13 9.53.53.png

@nuxtjs/axiosでbaseURL設定するには、nuxt.config.jsに下記のように書き込みます。

modules: [
    '@nuxtjs/axios',
  ],

  axios: {
    baseURL: 'http://localhost:8000',
  },

このように書き変わります。
スクリーンショット 2022-06-13 12.42.57.png

HerokuのBASE_URLの設定を確認して、
スクリーンショット 2022-06-14 9.36.44.png
nuxt.config.jsの設定を書き換えでBASE_URLに値(URL)を渡したら、

nuxt.config.js
 modules: [
    '@nuxtjs/axios',
  ],

  axios: {
    baseURL: process.env.BASE_URL
  },

プロダクション環境で動作する設定を下記コマンドで実行し、

heroku config:set HOST=0.0.0.0
heroku config:set NODE_ENV=production

ルートディレクトリにProcfileを追加しweb: nuxt startを書き込む。
スクリーンショット 2022-06-13 9.07.08.png
Nuxt.jsアプリをHerokuでプッシュしてデプロイすると。

git push heroku main

切り替わり、無事データが送られるようになったようです。
スクリーンショット 2022-06-13 9.12.04.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?