如何在 NGINX 中使用 process.env 构建 node.js 应用程序 运行
How to work with process.env with a build node.js app running in NGINX
我正在设置一个 dockerized 节点,MongoDB,Vue,Express 应用程序,我在构建我的应用程序后遇到问题。
对于客户端部分,我需要使用以下 Api.js
从应用程序环境中获取 API-URL
import axios from 'axios'
export default() => {
return axios.create({
baseURL: process.env.API_URL
})
我的docker-撰写(仅依赖):
front:
image: nginx
environment:
- NODE_ENV = 'production'
- API_URL=[hidden]
- PORT=80
api:
build: ./etc/server/.
environment:
- PORT=80
- DATABASE_URL=server-database:27017
此代码在 webpack 开发环境中运行良好,但在构建应用程序并将其放在不同的 NGINX 容器中后却无法运行。我还尝试从 NGINX 容器更改环境变量,但应用程序仍然看不到任何 process.env。
我也试了插件dotenv,也是空的process.env
有人知道吗?
谢谢
我最终将 webpack.plugins 选项添加到我的基本 webpack 配置中。这有助于找到环境变量。
plugins: [
new webpack.DefinePlugin({
'process.env': {
API_URL: JSON.stringify(process.env.API_URL),
},
}),
],
我正在设置一个 dockerized 节点,MongoDB,Vue,Express 应用程序,我在构建我的应用程序后遇到问题。
对于客户端部分,我需要使用以下 Api.js
从应用程序环境中获取 API-URL import axios from 'axios'
export default() => {
return axios.create({
baseURL: process.env.API_URL
})
我的docker-撰写(仅依赖):
front:
image: nginx
environment:
- NODE_ENV = 'production'
- API_URL=[hidden]
- PORT=80
api:
build: ./etc/server/.
environment:
- PORT=80
- DATABASE_URL=server-database:27017
此代码在 webpack 开发环境中运行良好,但在构建应用程序并将其放在不同的 NGINX 容器中后却无法运行。我还尝试从 NGINX 容器更改环境变量,但应用程序仍然看不到任何 process.env。
我也试了插件dotenv,也是空的process.env
有人知道吗?
谢谢
我最终将 webpack.plugins 选项添加到我的基本 webpack 配置中。这有助于找到环境变量。
plugins: [
new webpack.DefinePlugin({
'process.env': {
API_URL: JSON.stringify(process.env.API_URL),
},
}),
],