[React] Configure a React & Redux Application For Production Deployment and Deploy to Now

本文介绍如何通过配置.env文件及使用Now CLI将React应用程序部署到生产环境的过程。包括设置环境变量、更新package.json文件以及利用json-server进行本地数据模拟等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this lesson, we’ll make a few small changes to our scripts and add some environment variables that will be used at build time to get our application ready to be deployed to a production environment using the now service. Once properly configured, we’ll use the now CLI and publish our application to a production server.

 

React support .env file by default, add a .env file in the root folder:

REACT_APP_BASE_URL=http://localhost:9001/todos

which just holding our api configuration.

 

Also create a .env.production file:

REACT_APP_BASE_URL=./todos

Because we want json-server and our ui using the same domain & port, so here we can just use relative path.

 

To use the variable in .env, we can do:

const baseUrl = process.env.REACT_APP_BASE_URL;

export const getTodos = async () => {
    return await fetch(baseUrl)
        .then((response) => response.json());
};

 

Update package.json:

    "start": "json-server --static ./build db.json",
    "dev": "react-scripts start",

We change the original "start" to "dev".

Because after "build", there will be a "build" folder, so tell json-server to server the build folder and use db.json file a db.

 

After everything set up, just run:

now

 

转载于:https://www.cnblogs.com/Answer1215/p/7487019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值