【问题标题】:Porting Express App to Meteor将 Express 应用程序移植到 Meteor
【发布时间】:2015-11-18 04:59:45
【问题描述】:

我一直致力于将基于 Express 的基本应用程序移植到 Meteor。出色的帖子Is there an easy way to convert an express app to meteor? 是一个很好的开始,它使用服务员函数将 Meteor 期望的 Iron-router 路由包装到 Express 喜欢的 req / res 中。

但是,我遇到了一个我被困住的错误。我无法让 Meteor 将 res.render 对象传递给我的车把模板引擎。

例如:

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

/** 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 未定义。

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 的车把包来构建预编译的模板(下面的示例),但我没有任何运气让它正常工作

(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});
})();

即使是定义本地模板的最简单方法

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 中呈现的建议​​或示例将不胜感激。理想情况下,为了简单起见,我想使用实时部分而不是预编译代码。谢谢!!!

【问题讨论】:

    标签: node.js express meteor


    【解决方案1】:

    如果它是一个基本的应用程序,我建议重新开始,尽可能重复使用东西。通过为项目创建异步包装器会增加很多复杂性。

    【讨论】:

      【解决方案2】:

      为了让我之前的回答生效,您现在需要添加handlebars-server

      meteor add cmather:handlebars-server
      

      【讨论】:

        猜你喜欢
        • 2017-05-21
        • 2010-11-29
        • 2011-08-04
        • 2014-05-28
        • 2011-02-27
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多