2014-08-29 2 views
0

저는 Meteor에 기본 Express 기반 앱을 이식하는 작업을하고 있습니다. 우수한 게시물 Is there an easy way to convert an express app to meteor?은 Meteor가 Express가 좋아하는 req/res로 기대하는 철제 라우터 경로를 감싸기 위해 웨이터 기능을 사용하여 훌륭한 시작이었습니다.Express App을 Meteor로 이식하기

그러나 내가 붙어있는 버그를 맞았습니다. 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); 
}); 

이를

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

아래의 함수로 라우팅 res.render 위의 메시지에서 컴파일러는 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' 

Meteor/Express에서 렌더링하기 위해 JSON 문서 객체를 Handlebars에 성공적으로 전달할 수있는 방법에 대한 제안이나 사례는 많이 감사하겠습니다. 이상적으로, 나는 단순화를 위해 미리 컴파일 된 코드가 아닌 실시간 부분을 사용하고 싶다. 감사!!!

답변

1

기본 앱이라면 처음부터 다시 시작하고 가능한 한 재사용 할 것을 제안합니다. 프로젝트에 비동기 랩퍼를 작성하여 많은 복잡성을 추가 할 것입니다. 직장 내 이전 답변을 위해

0

, 당신은 지금 handlebars-server을 추가해야

meteor add cmather:handlebars-server