React 和 FLUX - 我应该在我的网络应用程序中的什么地方实施服务器例程?
React and FLUX - Where would I implement server routines in my web app?
我最近开始学习 MERN 堆栈(MongoDB、Express、React、Node)和 FLUX。我已经使用它大约 2 周了,我真的很喜欢它。
我对 FLUX 的理解是依赖于以下步骤:
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.
Dispatcher: dispatcher works like a global hub which triggers all the listeners rgistered to it whenever an action is sent to it.
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.
Views: Views are the html rendered components.
在我创建的一个简单的待办事项列表应用程序中,每个步骤都有自己的目录。
我的问题是:
如果我想 运行 一个单独的例程,例如页面加载例程,检查自上次记录更新以来是否已经过了一天,并根据 server/database 做一些事情那个逻辑。
例如,从页面加载调用此例程的最佳方式是什么?
感谢任何帮助或建议,提前致谢。
在我看来,最好的方法是让您的服务器路由为应用程序的页面提供服务,还包含检查记录和进行更新的逻辑(这 checking/updating 可能应该是一个异步过程为了不延迟应用程序的服务)。这样你就可以保持逻辑服务器端(这是可能的,因为它不依赖于你的用户 'doing' 除了请求页面之外的任何东西)。
编辑:详细说明
我假设在你的服务器代码中你有类似这样的东西:
app.get('/', function (req, res) {
res.send('index');
});
这会变成这样:
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();
}
在此处阅读有关节点子进程的更多信息:https://nodejs.org/api/child_process.html
EDIT2:重构代码
我最近开始学习 MERN 堆栈(MongoDB、Express、React、Node)和 FLUX。我已经使用它大约 2 周了,我真的很喜欢它。
我对 FLUX 的理解是依赖于以下步骤:
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.
Dispatcher: dispatcher works like a global hub which triggers all the listeners rgistered to it whenever an action is sent to it.
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.
Views: Views are the html rendered components.
在我创建的一个简单的待办事项列表应用程序中,每个步骤都有自己的目录。
我的问题是:
如果我想 运行 一个单独的例程,例如页面加载例程,检查自上次记录更新以来是否已经过了一天,并根据 server/database 做一些事情那个逻辑。
例如,从页面加载调用此例程的最佳方式是什么?
感谢任何帮助或建议,提前致谢。
在我看来,最好的方法是让您的服务器路由为应用程序的页面提供服务,还包含检查记录和进行更新的逻辑(这 checking/updating 可能应该是一个异步过程为了不延迟应用程序的服务)。这样你就可以保持逻辑服务器端(这是可能的,因为它不依赖于你的用户 'doing' 除了请求页面之外的任何东西)。
编辑:详细说明 我假设在你的服务器代码中你有类似这样的东西:
app.get('/', function (req, res) {
res.send('index');
});
这会变成这样:
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();
}
在此处阅读有关节点子进程的更多信息:https://nodejs.org/api/child_process.html
EDIT2:重构代码