Adding connection strings to a production app










-1















I have an app ready for production. For it to work, each client needs to set a unique url to access their data. How would i prepare the app for making it easy to add a url as an access point to the clients?



Would a correct way to do this be to add it in the manifest.json file and somehow reference it from there? (Until now in development i've only used a global URL in a js file)










share|improve this question






















  • A .env environment file or a aoo-config.js will help you here.

    – kiranvj
    Nov 14 '18 at 9:24
















-1















I have an app ready for production. For it to work, each client needs to set a unique url to access their data. How would i prepare the app for making it easy to add a url as an access point to the clients?



Would a correct way to do this be to add it in the manifest.json file and somehow reference it from there? (Until now in development i've only used a global URL in a js file)










share|improve this question






















  • A .env environment file or a aoo-config.js will help you here.

    – kiranvj
    Nov 14 '18 at 9:24














-1












-1








-1








I have an app ready for production. For it to work, each client needs to set a unique url to access their data. How would i prepare the app for making it easy to add a url as an access point to the clients?



Would a correct way to do this be to add it in the manifest.json file and somehow reference it from there? (Until now in development i've only used a global URL in a js file)










share|improve this question














I have an app ready for production. For it to work, each client needs to set a unique url to access their data. How would i prepare the app for making it easy to add a url as an access point to the clients?



Would a correct way to do this be to add it in the manifest.json file and somehow reference it from there? (Until now in development i've only used a global URL in a js file)







reactjs production






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:15









melbilmelbil

356




356












  • A .env environment file or a aoo-config.js will help you here.

    – kiranvj
    Nov 14 '18 at 9:24


















  • A .env environment file or a aoo-config.js will help you here.

    – kiranvj
    Nov 14 '18 at 9:24

















A .env environment file or a aoo-config.js will help you here.

– kiranvj
Nov 14 '18 at 9:24






A .env environment file or a aoo-config.js will help you here.

– kiranvj
Nov 14 '18 at 9:24













2 Answers
2






active

oldest

votes


















1














You need to install the package dotenv package and create a .env file in your root directory which should contain your environment variables.



Assuming that the URl you are referring to is http://localhost:3000/some/url on your localhost, then your .env file might look like:



MY_URL=http://localhost:3000/some/url


Then in your react application, you can get the value of MY_URL by doing:



const url = process.env.MY_URL


Note that if you are using the create-react-app package, then you do not need to install the dotenv package since it already comes with the create-react-app package. Also you need to change it:



REACT_APP_MY_URL=http://localhost:3000/some/url


Also make sure to add the .env file to your .gitignore file so that you do not push it to your repo.



Assuming that you are deploying your application to Heroku. Heroku provides a simple interface which allows you to add your environment variables which looks like:



enter image description here



That's it.






share|improve this answer























  • This would work if i only had to declare the variable at build time, but i need this to work at runtime

    – melbil
    Nov 14 '18 at 12:08











  • Not true, this will work both at build time or runtime. Can you explain what you mean?

    – lomse
    Nov 14 '18 at 12:16











  • I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

    – melbil
    Nov 14 '18 at 12:20












  • Which platform are you deploying your app to?

    – lomse
    Nov 14 '18 at 13:42






  • 1





    So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

    – lomse
    Nov 14 '18 at 15:19


















0














Maybe you could store them in environment variables?



that way you can always edit them later without having to change components.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296611%2fadding-connection-strings-to-a-production-app%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to install the package dotenv package and create a .env file in your root directory which should contain your environment variables.



    Assuming that the URl you are referring to is http://localhost:3000/some/url on your localhost, then your .env file might look like:



    MY_URL=http://localhost:3000/some/url


    Then in your react application, you can get the value of MY_URL by doing:



    const url = process.env.MY_URL


    Note that if you are using the create-react-app package, then you do not need to install the dotenv package since it already comes with the create-react-app package. Also you need to change it:



    REACT_APP_MY_URL=http://localhost:3000/some/url


    Also make sure to add the .env file to your .gitignore file so that you do not push it to your repo.



    Assuming that you are deploying your application to Heroku. Heroku provides a simple interface which allows you to add your environment variables which looks like:



    enter image description here



    That's it.






    share|improve this answer























    • This would work if i only had to declare the variable at build time, but i need this to work at runtime

      – melbil
      Nov 14 '18 at 12:08











    • Not true, this will work both at build time or runtime. Can you explain what you mean?

      – lomse
      Nov 14 '18 at 12:16











    • I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

      – melbil
      Nov 14 '18 at 12:20












    • Which platform are you deploying your app to?

      – lomse
      Nov 14 '18 at 13:42






    • 1





      So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

      – lomse
      Nov 14 '18 at 15:19















    1














    You need to install the package dotenv package and create a .env file in your root directory which should contain your environment variables.



    Assuming that the URl you are referring to is http://localhost:3000/some/url on your localhost, then your .env file might look like:



    MY_URL=http://localhost:3000/some/url


    Then in your react application, you can get the value of MY_URL by doing:



    const url = process.env.MY_URL


    Note that if you are using the create-react-app package, then you do not need to install the dotenv package since it already comes with the create-react-app package. Also you need to change it:



    REACT_APP_MY_URL=http://localhost:3000/some/url


    Also make sure to add the .env file to your .gitignore file so that you do not push it to your repo.



    Assuming that you are deploying your application to Heroku. Heroku provides a simple interface which allows you to add your environment variables which looks like:



    enter image description here



    That's it.






    share|improve this answer























    • This would work if i only had to declare the variable at build time, but i need this to work at runtime

      – melbil
      Nov 14 '18 at 12:08











    • Not true, this will work both at build time or runtime. Can you explain what you mean?

      – lomse
      Nov 14 '18 at 12:16











    • I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

      – melbil
      Nov 14 '18 at 12:20












    • Which platform are you deploying your app to?

      – lomse
      Nov 14 '18 at 13:42






    • 1





      So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

      – lomse
      Nov 14 '18 at 15:19













    1












    1








    1







    You need to install the package dotenv package and create a .env file in your root directory which should contain your environment variables.



    Assuming that the URl you are referring to is http://localhost:3000/some/url on your localhost, then your .env file might look like:



    MY_URL=http://localhost:3000/some/url


    Then in your react application, you can get the value of MY_URL by doing:



    const url = process.env.MY_URL


    Note that if you are using the create-react-app package, then you do not need to install the dotenv package since it already comes with the create-react-app package. Also you need to change it:



    REACT_APP_MY_URL=http://localhost:3000/some/url


    Also make sure to add the .env file to your .gitignore file so that you do not push it to your repo.



    Assuming that you are deploying your application to Heroku. Heroku provides a simple interface which allows you to add your environment variables which looks like:



    enter image description here



    That's it.






    share|improve this answer













    You need to install the package dotenv package and create a .env file in your root directory which should contain your environment variables.



    Assuming that the URl you are referring to is http://localhost:3000/some/url on your localhost, then your .env file might look like:



    MY_URL=http://localhost:3000/some/url


    Then in your react application, you can get the value of MY_URL by doing:



    const url = process.env.MY_URL


    Note that if you are using the create-react-app package, then you do not need to install the dotenv package since it already comes with the create-react-app package. Also you need to change it:



    REACT_APP_MY_URL=http://localhost:3000/some/url


    Also make sure to add the .env file to your .gitignore file so that you do not push it to your repo.



    Assuming that you are deploying your application to Heroku. Heroku provides a simple interface which allows you to add your environment variables which looks like:



    enter image description here



    That's it.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 9:53









    lomselomse

    1,30442646




    1,30442646












    • This would work if i only had to declare the variable at build time, but i need this to work at runtime

      – melbil
      Nov 14 '18 at 12:08











    • Not true, this will work both at build time or runtime. Can you explain what you mean?

      – lomse
      Nov 14 '18 at 12:16











    • I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

      – melbil
      Nov 14 '18 at 12:20












    • Which platform are you deploying your app to?

      – lomse
      Nov 14 '18 at 13:42






    • 1





      So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

      – lomse
      Nov 14 '18 at 15:19

















    • This would work if i only had to declare the variable at build time, but i need this to work at runtime

      – melbil
      Nov 14 '18 at 12:08











    • Not true, this will work both at build time or runtime. Can you explain what you mean?

      – lomse
      Nov 14 '18 at 12:16











    • I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

      – melbil
      Nov 14 '18 at 12:20












    • Which platform are you deploying your app to?

      – lomse
      Nov 14 '18 at 13:42






    • 1





      So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

      – lomse
      Nov 14 '18 at 15:19
















    This would work if i only had to declare the variable at build time, but i need this to work at runtime

    – melbil
    Nov 14 '18 at 12:08





    This would work if i only had to declare the variable at build time, but i need this to work at runtime

    – melbil
    Nov 14 '18 at 12:08













    Not true, this will work both at build time or runtime. Can you explain what you mean?

    – lomse
    Nov 14 '18 at 12:16





    Not true, this will work both at build time or runtime. Can you explain what you mean?

    – lomse
    Nov 14 '18 at 12:16













    I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

    – melbil
    Nov 14 '18 at 12:20






    I have a unejected create-react-app with a .env pointing to a location. I then do a 'npm run build' and get a bunch of files produced in the build folder. Then i run 'serve -s build'. And here is my problem - as i do not know the actual url the app will be using, i have to inject it into the app at a later stage. Currently it points to the url used in development, but this needs to be changed. How would i do that? I tried creating a new .env file in build folder, but no luck

    – melbil
    Nov 14 '18 at 12:20














    Which platform are you deploying your app to?

    – lomse
    Nov 14 '18 at 13:42





    Which platform are you deploying your app to?

    – lomse
    Nov 14 '18 at 13:42




    1




    1





    So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

    – lomse
    Nov 14 '18 at 15:19





    So you can start by renaming the .env file to env.sample which will contain dummy data is can be pushed to your repo. In your here-is-how-you-connect-the-app-to-your-database file, let the user know that they need to replace the dummy values with the appropriate values and add them to Azure. You can check how to set environment variables on Azure docs.microsoft.com/en-us/azure/container-instances/…

    – lomse
    Nov 14 '18 at 15:19













    0














    Maybe you could store them in environment variables?



    that way you can always edit them later without having to change components.






    share|improve this answer



























      0














      Maybe you could store them in environment variables?



      that way you can always edit them later without having to change components.






      share|improve this answer

























        0












        0








        0







        Maybe you could store them in environment variables?



        that way you can always edit them later without having to change components.






        share|improve this answer













        Maybe you could store them in environment variables?



        that way you can always edit them later without having to change components.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 9:20









        Teun van der WijstTeun van der Wijst

        570315




        570315



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296611%2fadding-connection-strings-to-a-production-app%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3