将 Express 应用程序移植到 Meteor [英] Porting Express App to Meteor

查看:50
本文介绍了将 Express 应用程序移植到 Meteor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于将基于 Express 的基本应用程序移植到 Meteor.优秀的帖子 是否有一种简单的方法可以将快速应用程序转换为流星?是一个很好的开始,它使用了一个服务员功能将 Meteor 期望的 Iron-router 路由包装到 Express 喜欢的 req/res 中.

I have been working on porting a basic Express based app to Meteor. The excellent post Is there an easy way to convert an express app to meteor? was a great start, using a waiter function to wrap iron-router routes that Meteor expects into req / res that Express likes.

但是,我遇到了一个我一直坚持的错误.我无法让 Meteor 将 res.render 对象传递给我的把手模板引擎.

However, I've hit a bug that I'm stuck on. I'm not able to get Meteor to pass the res.render object over to my handlebars templating engine.

例如:

main.js

app.get('/complex', function(req, res){
  var data = {
    name: 'Gorilla',
    address: {
        streetName: 'Broadway',
        streetNumber: '721',
        floor: 4,
        addressType: {
        typeName: 'residential'
      }
    }
  };
res.render('complex', data); 
});

通过iron-router调用/complex路由时,路由到下面的函数res.render

When the /complex route is called via iron-router, it is routed to the function res.render below

/** 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);
        };

        console.log(name); // complex
        console.log(data); // Gorilla...
        var html = Handlebars.templates[name](data); // THIS ERRORS OUT
        html = JSON.stringify(name) + " " + JSON.stringify(data) // WORKS
        callback(null, html);
    };
    ...

在上面的消息中,编译器错误地指出 Handlebars 未定义.

In the message above, the compiler errors out saying that Handlebars is undefined.

W20140828-22:47:49.439(-7)? (STDERR) TypeError: Cannot call method 'complex' of undefined
W20140828-22:47:49.439(-7)? (STDERR)     at ServerResponse.res.render (app/server/myapp.js:57:50)
W20140828-22:47:49.440(-7)? (STDERR)     at app/server/myapp.js:298:25

我使用 NPM 的 handlebars 包来构建预编译模板(下面的示例),但我没有运气让它正常工作

I used NPM's handlebars package to build pre-compiled templates (example below), but I haven't had any luck getting it to work correctly

(function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
  templates['complex'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  var helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression;
  return "\n<p>\nThe data that was passed to `res.render` is:\n<code>var data = {name: 'Gorilla'};</code>\n</p>\n\n<p>\nWe can display the value of <em>name</em> using <code>&#123;&#123;name&#125;&#125;</code>, which results in: <b>"
  + escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper)))
  + "</b>.\n</p>\n";
},"useData":true});
})();

即使是定义本地模板的最简单路线

Even going the simplest route of defining a local template

query_string = "<code>var data = {name: 'Gorilla'};</code><p>{{data}}</p>"
template = Handlebars.compile(query_string)

导致错误:

W20140828-21:51:47.126(-7)? (STDERR) TypeError: Object function exphbs(config) {
W20140828-21:51:47.128(-7)? (STDERR)     return exphbs.create(config).engine;
W20140828-21:51:47.129(-7)? (STDERR) } has no method 'compile'

关于如何成功地将 JSON 文档对象传递给 Handlebars 以在 Meteor/Express 中呈现的任何建议或示例将不胜感激.理想情况下,为了简单起见,我想使用实时部分而不是预编译代码.谢谢!!!

Any suggestions or examples about how I can successfully pass a JSON document object off to Handlebars for rendering inside Meteor/Express would be much appreciated. Ideally, I'd like to use real-time partials and not pre-compiled code for simplicity. Thanks!!!

推荐答案

如果它是一个基本的应用程序,我建议您从头开始,重复使用您可以使用的东西.为项目创建异步包装器会增加很多复杂性.

If it's a basic app I'd suggest just starting over, reusing stuff you can. You'll add a lot of complexity by creating async wrappers to your project.

这篇关于将 Express 应用程序移植到 Meteor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆