reactjs - How to set different basepath for images in production and development in React
问题描述
I'm retrieving my images on production as:
src="/dibble_back_end/images/icon.png"
Which then is proxied through my nginx:
location /dibble_back_end {
proxy_pass http://dibble_back_end:4000;
rewrite ^/dibble_back_end(.*)$ $1 break;
}
The issue is that on development react server is running in ExpressJS not on ngInx. I'd want to based on environment variable be able to switch between production and development.
e.g.
Dev:
src="localhost:4000/images/icon.png"
Production:
src="/dibble_back_end/images/icon.png"
解决方案
您没有提供有关设置的详细信息,但如果您使用的是Create a React App,则可以使用NODE_ENV
environment variable,例如:
const src = process.env.NODE_ENV === 'production' ? "/dibble_back_end/images/icon.png" : "localhost:4000/images/icon.png"
或者
const basePath = process.env.NODE_ENV === 'production' ? "/dibble_back_end" : "localhost:4000";
const src = `${src}/images/icon.png`;
推荐阅读
- python - 自定义数据集、数据加载器、采样器或其他?
- java - Apache Commons Codec Base64 是 sun.misc.BASE64 的替代品吗?
- c++ - 有没有办法在 C++ 中显式调用可选参数?
- python - 启用对 opencv 的 cuda 支持
- c# - 为什么 aspnetcore 服务响应时间会随着并发客户端数量的增加而增加?
- php - Silverstripe 4 - 如何查询从表中获取所有数据并将字段添加到 cmsField
- react-native - React Native 项目返回“TypeError: undefined is not an object”
- android - 安卓谷歌支付集成
- node.js - Mongoose - 如果有空参数,我如何告诉猫鼬跳过空参数
- python - 使用 Popen.wait() 时遇到死锁