节点胡须渲染服务器端

我试图创build一个简单的应用程序来编译胡须模板到静态页面服务器端,这是我到目前为止:

var view = { title: "Joe", calc: function () { return 2+4; } }; var mustache = require("mustache"); var template = require("./home.template"); var output = mustache.to_html(template, view); console.log(output); 

而我的模板看起来像:

 {{title}} spend {{calc}} 

任何build议是什么导致这个失败?

这里是完整的错误信息:

 home.template:1 } spend {{calc}} ^ module.js:437 var compiledWrapper = runInThisContext(wrapper, filename, true); ^ SyntaxError: Unexpected token { at Module._compile (module.js:437:25) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:362:17) at require (module.js:378:17) at Object.<anonymous> (/Users/MorehouseJ09/Documents/production_development/mustache/current/compiler.js:12:16) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) 

任何帮助将是伟大的!

使用fs.readFile()以stringforms读取模板。 要求不会工作,除非它需要JavaScript代码,而不是胡须代码。

http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback

编辑
看看这个工程…

 var mustache = require("mustache"); var fs = require("fs"); var view = { title: "Joe", calc: function () { return 2+4; } }; fs.readFile('./home.template', 'utf-8', function (err, data) { if (err) throw err; var output = mustache.to_html(data, view); console.log(output); });