Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

docker - Nuxt.js environment variables behave differently for nuxt.config.js than in ~/api/index.js context

I have an issue with environment variables I do not quite understand. I pass my environment variables via Cloud Run:

USER_SERVICE_URL
https://user-service-av3y6faeqq-ew.a.run.app
BASEURL
https://prototype-spa-av3y6faeqq-ew.a.run.app

There are two environment variables I call to do two different things.

// ~/api/index.js
const USER_SERVICE_URL = process.env.USER_SERVICE_URL || 'http://localhost:8080'

This is used for proxy calls to the user-service and works as expected. Now I also want to set the baseUrl of my axios to what the environment variable BASEURL (see above) has defined. I there added this to the nuxt.config.js

// nuxt.config.js
  env: {
    baseUrl : process.env.BASEURL || 'http://localhost:3000',
  },

And in my axios plugin I do this:

// ~/plugins/axios.js
export default function ({ $axios }) {
  $axios.defaults.timeout = 5000;
  $axios.defaults.baseURL = process.env.baseUrl || 'http://axios.plugin.failed';
}

The result is that my baseUrl is set to http://localhost:3000 which is the fallback result of the configuration in the nuxt.config.js file. What is there difference here? What confuses me the most is that it does work locally. I set export BASEURL=https://this.works.com and the baseUrl is properly changed to that.

The different between local and production is the usage of the docker container. Here is my Dockerfile. Be aware that docker somewhat can't be the problem since USER_SERVICE_URL=https://user-service-av3y6faeqq-ew.a.run.app is working properly.

# ~/Dockerfile
FROM node:11.13.0-alpine

# create destination directory
# build necessary, even if no static files are needed,
# since it builds the server as well
RUN npm run build

EXPOSE 80

# set app serving to permissive / assigned
ENV NUXT_HOST=0.0.0.0
# set app port
ENV NUXT_PORT=80

# start the app
CMD [ "npm", "start" ]
question from:https://stackoverflow.com/questions/65917319/nuxt-js-environment-variables-behave-differently-for-nuxt-config-js-than-in-ap

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...