Recommended way for using Express.js with React.js?











up vote
1
down vote

favorite












I have found two ways to combine React and Express:



[OPTION 1] ExpressJS serve React static files



Fundamentally, the idea here is to have pre-compiled JavaScript files at your disposal before spinning up the server. And then in Express middleware:



app.use(express.static(path.join(__dirname, '../client/build')));



And then in tandem have other endpoints that execute logic on server-side.



[OPTION 2] Host React files separately



Basically, decouple the logic in entirety of Frontend and Backend with one server just for serving static JS files. And then another Express server for running any queries against the database. And returning simple JSON responses.



Which one is the recommended way? Is there an advantage of using one approach over the other?










share|improve this question






















  • I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
    – Daniel
    Nov 8 at 3:32










  • I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
    – whatever123
    Nov 8 at 4:20















up vote
1
down vote

favorite












I have found two ways to combine React and Express:



[OPTION 1] ExpressJS serve React static files



Fundamentally, the idea here is to have pre-compiled JavaScript files at your disposal before spinning up the server. And then in Express middleware:



app.use(express.static(path.join(__dirname, '../client/build')));



And then in tandem have other endpoints that execute logic on server-side.



[OPTION 2] Host React files separately



Basically, decouple the logic in entirety of Frontend and Backend with one server just for serving static JS files. And then another Express server for running any queries against the database. And returning simple JSON responses.



Which one is the recommended way? Is there an advantage of using one approach over the other?










share|improve this question






















  • I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
    – Daniel
    Nov 8 at 3:32










  • I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
    – whatever123
    Nov 8 at 4:20













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have found two ways to combine React and Express:



[OPTION 1] ExpressJS serve React static files



Fundamentally, the idea here is to have pre-compiled JavaScript files at your disposal before spinning up the server. And then in Express middleware:



app.use(express.static(path.join(__dirname, '../client/build')));



And then in tandem have other endpoints that execute logic on server-side.



[OPTION 2] Host React files separately



Basically, decouple the logic in entirety of Frontend and Backend with one server just for serving static JS files. And then another Express server for running any queries against the database. And returning simple JSON responses.



Which one is the recommended way? Is there an advantage of using one approach over the other?










share|improve this question













I have found two ways to combine React and Express:



[OPTION 1] ExpressJS serve React static files



Fundamentally, the idea here is to have pre-compiled JavaScript files at your disposal before spinning up the server. And then in Express middleware:



app.use(express.static(path.join(__dirname, '../client/build')));



And then in tandem have other endpoints that execute logic on server-side.



[OPTION 2] Host React files separately



Basically, decouple the logic in entirety of Frontend and Backend with one server just for serving static JS files. And then another Express server for running any queries against the database. And returning simple JSON responses.



Which one is the recommended way? Is there an advantage of using one approach over the other?







node.js reactjs express webpack microservices






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 18:30









whatever123

6217




6217












  • I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
    – Daniel
    Nov 8 at 3:32










  • I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
    – whatever123
    Nov 8 at 4:20


















  • I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
    – Daniel
    Nov 8 at 3:32










  • I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
    – whatever123
    Nov 8 at 4:20
















I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
– Daniel
Nov 8 at 3:32




I have not heard of a recommended way and I have built several Express with React apps. What does your app do? What is your goal?
– Daniel
Nov 8 at 3:32












I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
– whatever123
Nov 8 at 4:20




I am working on an app that already exists in production. But I was wondering if they took the correct approach. The app, itself, has two portals depending on the type of user (think teacher/student exchanging homeworks). In addition to that, it also has another app that has a separate frontend (think code submission environment).
– whatever123
Nov 8 at 4:20












1 Answer
1






active

oldest

votes

















up vote
2
down vote













This is an opinionated question, and so I can only give an opinionated response.



I personally recommend OPTION 1 because you'll run into Cross-Origin-Resource-Sharing (CORS) issues in a number of places when you decouple your front and back ends. This isn't to say that it can't be done, but it will be an annoying thing you'll have to deal with.



With option 2 you'll also most likely have to be sending any requests to your back end with absolute URL paths, which can be very tough to maintain when an application scales.



With option 1 you'll have more flexibility, less to maintain, and less annoying workarounds to implement.






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',
    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%2f53195626%2frecommended-way-for-using-express-js-with-react-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    This is an opinionated question, and so I can only give an opinionated response.



    I personally recommend OPTION 1 because you'll run into Cross-Origin-Resource-Sharing (CORS) issues in a number of places when you decouple your front and back ends. This isn't to say that it can't be done, but it will be an annoying thing you'll have to deal with.



    With option 2 you'll also most likely have to be sending any requests to your back end with absolute URL paths, which can be very tough to maintain when an application scales.



    With option 1 you'll have more flexibility, less to maintain, and less annoying workarounds to implement.






    share|improve this answer

























      up vote
      2
      down vote













      This is an opinionated question, and so I can only give an opinionated response.



      I personally recommend OPTION 1 because you'll run into Cross-Origin-Resource-Sharing (CORS) issues in a number of places when you decouple your front and back ends. This isn't to say that it can't be done, but it will be an annoying thing you'll have to deal with.



      With option 2 you'll also most likely have to be sending any requests to your back end with absolute URL paths, which can be very tough to maintain when an application scales.



      With option 1 you'll have more flexibility, less to maintain, and less annoying workarounds to implement.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        This is an opinionated question, and so I can only give an opinionated response.



        I personally recommend OPTION 1 because you'll run into Cross-Origin-Resource-Sharing (CORS) issues in a number of places when you decouple your front and back ends. This isn't to say that it can't be done, but it will be an annoying thing you'll have to deal with.



        With option 2 you'll also most likely have to be sending any requests to your back end with absolute URL paths, which can be very tough to maintain when an application scales.



        With option 1 you'll have more flexibility, less to maintain, and less annoying workarounds to implement.






        share|improve this answer












        This is an opinionated question, and so I can only give an opinionated response.



        I personally recommend OPTION 1 because you'll run into Cross-Origin-Resource-Sharing (CORS) issues in a number of places when you decouple your front and back ends. This isn't to say that it can't be done, but it will be an annoying thing you'll have to deal with.



        With option 2 you'll also most likely have to be sending any requests to your back end with absolute URL paths, which can be very tough to maintain when an application scales.



        With option 1 you'll have more flexibility, less to maintain, and less annoying workarounds to implement.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 21:45









        Mike Abeln

        1586




        1586






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195626%2frecommended-way-for-using-express-js-with-react-js%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud