有一个简单的方法来转换一个快速的应用程序meteor?

我正在尝试将一个node.js与express框架应用程序转换为meteor。 本质上是做相反的方式https://github.com/onmodulus/demeteorizer

绝对不是自动的,但有一些技巧,你可以链接到几乎自动得到它。

我已经经历了这一切,这里是我的所有技巧。

让我们从您的快速应用程序main.js文件开始。 这一个你需要在顶部添加以下内容:

/server/main.js

 routes = {}; var app = { get: function(route, foo) { // routes.get[route] = foo; routes[route] = foo; }, all: function(route, foo) { // routes.all[route] = foo; routes[route] = foo; } }; 

所有这一切都是定义你需要的appfunction,并将定义的路由logging在一个对象中,我们稍后将使用它来定义这些使用iron-router 。 所以这确保了像下面这样的东西logging在routes

/server/main.js

 app.get('/show', function(req, res) { res.render('mytemplate'); }); 

这真是主要的伎俩 。 从这里开始它的正义劳动。

在良好的meteor风格中,我们将把所有的路由渲染调用包装成一个光纤,使它们像meteor服务器上的其他所有东西一样同步。 为此,我们定义了一个可以重复使用的包装函数waiter来包装路由函数。 当我们添加它的时候,我们将按照meteor服务器上的铁路路由获得的连接请求和响应转化为resreq对象表示希望看到的。 请注意,这不是完整的。 这只是我想从这些对象中使​​用的签名。

/server/main.js

 /** create an sync version for meteor */ waiter = function(foo, req, res) { var waiter_aux = Meteor._wrapAsync(function(foo, req, res, callback) { res.set = function(header, value) { res.setHeader(header, value); }; res.send = function(codeorhtml, html) { if (html) { // two arguments provided, treat as described res.statusCode = codeorhtml; } else { // no code, just html html = codeorhtml; } callback(null, html); }; res.render = function(name, data, callback) { callback = callback || function(err, html) { res.send(html); }; var html = Handlebars.templates[name](data); callback(null, html); }; res.json = function(object) { res.send(JSON.stringify(object)); } res.redirect = function(URL) { res.writeHead(302, { 'Location': URL }); res.end(); }; req.header = function(x) { return this.header[x]; }; TemplatesObject = Handlebars.templates; // these objects need to be extended further foo(req, res); }); return waiter_aux(foo, req, res); }; 

最后,真正的交易:为每个指定的快速路线创build路线。 为此,我们将使用铁路由器 。 下面的代码将遍历每个定义的路由(由我们重新定义的app函数捕获并存储在routes ),并使用我们的waiter将其包装在光纤中,这也将处理this.request / this.responsereqres应用程序假设的对象。

/routes.js

 if (Meteor.isServer) { // create routes for all the app.get's and app.all's in bibbase.js // (server) console.log("setting routes:", routes); _.each(routes, function(foo, route) { Router.map(function () { this.route(route, { path: route, where: 'server', action: function() { this.request.params = this.params; var html = waiter(foo, this.request, this.response); if (!this.response.statusCode) { this.response.statusCode = 200; } if (!this.response.getHeader('Content-Type')) { this.response .setHeader('Content-Type', 'text/html'); } this.response.end(html); } }); }); }); } 

这些是我为完成你所问的事情所做的最重要的事情。 我确定我在这里错过了一些细节,但这应该给你一个想法。


更新post-Spacebars(我忘了​​是哪个版本的meteor):

为了使这个工作,你现在需要添加handlebars-server :

 meteor add cmather:handlebars-server