How to make create-react-app auto build?

后端 未结 5 1901
傲寒
傲寒 2020-12-14 09:21

I have been using create react app for a while. \'npm start\' or \'yarn start\' autoreloads works fine by itself but now I have an another problem. Currently I run the app o

5条回答
  •  执念已碎
    2020-12-14 09:57

    Run your backend on a different port. If your running on express modify the file bin/www

    var port = process.env.PORT || 9000
    

    and in your /src folder create a config file where you configure your api host,routes, params etc

    //config/index.js
    export const Config = {
       protocol: 'http',
       host: window.location.hostname, //or the environment variable
       params: '/api/',
       api: {post:'/posts/'}
    }
    

    and in your calling component or where ever your calling the api's

    import {Config} from '../config'
    
    axios.get(`${Config.protocol}${Config.host}${Config.params}${Config.api.posts}${some id i guess}`)
    

提交回复
热议问题