danjonescidtrix danjonescidtrix - 1 year ago 69
Node.js Question

React and FLUX - Where would I implement server routines in my web app?

I recently started learning the MERN stack (MongoDB, Express, React, Node) and FLUX. Ive been using it for around 2 weeks now and I have been really enjoying it.

My understanding of FLUX is that is rely's on the following steps:

  1. Actions: these are the payloads with some data and some context (type) in short objects, these are created by some helper functions as
    a result of an activity in the view(mostly). For example when user
    clicks on an add button, we will create an action which will contain
    infomation to be added and the context. All actions are sent to the

  2. Dispatcher: dispatcher works like a global hub which triggers all the listeners rgistered to it whenever an action is sent to it.

  3. Stores: stores register themselves with dispatchers, when dispatcher broadcasts an actions arrival, stores update themselves if
    that action is relavant to those stores, and emit a change event which
    causes UI to get updated.

  4. Views: Views are the html rendered components.

Each of these steps has its own directory in a simple todo list app I have created.

My question is:

If I wanted to run a separate routine, for example a routine on page load that checks whether a day has passed since the last record updated, and do something on the server/database based on that logic.

What would be the best way to call this routine from a page load for example?

Any help or advice is appreciated, Thank you in advance.

Answer Source

The best way to do this seems to me having your server's route that serves the page with the app also contain the logic to check the record and do the update (this checking/updating should probably be an async process as to not delay the serving of the app). That way you keep the logic serverside (which is possible since it doesn't rely on your user 'doing' anything other than requesting the page).

EDIT: to elaborate I assume somehwere in your server code you have something similar to this:

app.get('/', function (req, res) {

this would become something like:

app.get('/', function (req, res) {
    //make child process that calls someServerRoutine(req);


    var record = getRecordBasedOnSomeRequestData(req);
    if(someCheckOnRecord(record)) doSomething();

read more about node childprocesses here: https://nodejs.org/api/child_process.html

EDIT2: restructured code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download