2015-09-16 39 views
0

我正在編寫NodeJS應用程序,該應用程序使用Handlebars爲一些已編譯的HTML提供服務。Handlebars nodejs獲取錯誤

express = require 'express' 
handlebars = require 'handlebars' 
fs = require 'fs' 

express().get '/foo.html', -> 
    fooData = {isFoo:true} 

    template = handlebars.compile fs.readFileSync('foo-template.html').toString() 

    htmlString = template(data) 
    res.send htmlString 

但是,我的foo.html文件有一些錯誤。

<html> 
    {{#if isFoo}} 
     What's up foo 
    <!-- closing {{/if}} tag is missing --> 
</html> 

當我訪問「/foo.html」,服務器似乎很好地工作,但我的要求只是掛起。有些事情一定是出了問題。 爲什麼不是Handlebars拋出某種異常?

或者,我如何確保Handlebars在我編寫foo.html時拋出異常?

回答

0

Handlebars或Express如何處理錯誤沒有任何問題。問題是我正確使用了https://github.com/kriskowal/q。 Q吃任何錯誤,並將其發送到其.catch ->

實際代碼段更多的東西是這樣的:

express = require 'express' 
handlebars = require 'handlebars' 
fs = require 'fs' 
Q = require 'q' 

doStuff = -> 
    def = Q.defer() 
    doAsynchronousThings -> 
    def.resolve() 
    def.promise 

express().get '/foo.html', -> 

    doStuff().then -> 

    fooData = {isFoo:true} 

    template = handlebars.compile fs.readFileSync('foo-template.html').toString() 

    htmlString = template(data) 
    res.send htmlString 

    # here is what I should have included 
    #.catch (e) -> 
    # console.log e