0

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 dispacher.

  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.

2
  • Why would you do that in the UI? If you need to check some data in a database and then update some other data in the database then the whole process can stay on the server, maybe through a cron job. Commented Oct 21, 2016 at 9:10
  • Ahh I see, That makes sense. What would be the best way to call this server routine from a page load for example? @SergiuParaschiv Commented Oct 21, 2016 at 9:11

1 Answer 1

1

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) {
    res.send('index');
});

this would become something like:

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

    res.send('index');
});

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.