如何在Node/Express中实现HMVC结构 [英] How to implement HMVC structure in Node/Express
问题描述
我的应用程序中有多个"api"端点,前端框架对其AJAX进程使用了这些端点.出于组织目的,我想重复使用这些终结点的代码来检索用于服务器端呈现的数据,在某些情况下,我会这样做以与搜索引擎完美配合.因此,理想情况下,我将实现某种HMVC设置以仅访问API端点上的路由.具体来说,我想在发布顶级响应之前获取响应并对其执行其他操作(例如,渲染带有结果的视图).
I have several "api" endpoints in my application that are used by the front-end framework for its AJAX processes. For organizational purposes, I would like to re-use the code for these endpoints to retrieve data for server-side rendering, which I do in some instances to play nicely with search engines. So, ideally I would implement some sort of HMVC setup to simply access a route on the API endpoint. Specifically I would like to get the response and perform additional actions on it before issuing the top-level response (e.g., render a view with results).
例如:
app.get('/post/recent', function(req, res) {
app.doRequest('/api/posts/', req, function(res2) {
var data = res2.body;
res.render('posts/index', data);
});
});
做到这一点的最佳方法是什么?
What's the best way to do this?
到目前为止,我想出的最好的选择是:
So far the best option I've come up with is:
将端点中的所有逻辑公开为一种方法,该方法将在app.get('...', myFunction)
中使用,然后可以在该路径的表达流之外的其他地方调用myFunction
. 但是,除非我编写了自己的不依赖于express的中间件实现,否则这将无法为我提供可靠的方式来运行特定于端点的中间件(我也想在HMVC请求上运行). .该API端点具有执行if(!hasAccess) res.send(403)
之类的中间件,我特别不想在我的主要路线中发生这种情况,因为我想呈现一个漂亮的错误页面,而不仅仅是发送错误代码.
Expose all logic in an endpoint as a method, which would be used in app.get('...', myFunction)
, and then I could call myFunction
elsewhere outside of the express flow for that path. However, this would not give me a reliable way to run middleware specific to the endpoint (which I would also want to run on the HMVC request) unless I wrote my own middleware implementation that did not rely on express. The API endpoint has middleware that does something like if(!hasAccess) res.send(403)
, which I specifically do NOT want to happen in my main route since I'd want to render a nice error page instead of just sending an error code.
示例:
var getPosts = function(req) {
var deferred = q.defer()
doDatabaseQuery(req.query).then(function(response) {
deferred.resolve(response)
});
};
app.get('/api/posts', myMiddlewareFunction(), function(req, res) {
getPosts(req).then(function(response) {
res.send(response);
});
);
app.get('/post/recent', function(req, res) {
// I want to run middleware here, not in root level
getPosts(req).then(function(response) {
res.render('post/index', response);
}, function(err) {
res.render('error/noaccess');
});
});
还有更好的主意吗?有没有办法以编程方式访问快速路线并获得结果?
Any better ideas? Is there a way to programmatically access an express route and get the result?
推荐答案
我通过研究Express源代码来解决这个问题.有一种名为handle
的方法,可以通过修改后的request
和response
对象手动调用该方法以获得我想要的效果:
I figured this out by diving into the Express source code. There is a method called handle
which I can manually invoke with modified request
and response
objects to get the effect I want:
app.get '/posts', (req, res) ->
req.url = '/api/posts'
newRes = _.clone(res)
newRes.send = (data, code)->
if code is 200
return res.render('posts/index', data)
else
return res.render('error')
app.handle(req, newRes)
这篇关于如何在Node/Express中实现HMVC结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!