Javascript 节点js require无法正常工作
我正在开发一个node应用程序,但node不需要正确地使用我的类。相反,它返回“{}” 为了测试所包含的内容,我尝试打印出 'require./core/template',但它返回'{}':Javascript 节点js require无法正常工作,javascript,node.js,coffeescript,require,Javascript,Node.js,Coffeescript,Require,我正在开发一个node应用程序,但node不需要正确地使用我的类。相反,它返回“{}” 为了测试所包含的内容,我尝试打印出 'require./core/template',但它返回'{}': require './core/template' res.end util.inspect template 当我用例如res.end“hello world”替换这一行时,我得到以下错误: $ /Users/Filipe/Desktop/Smoothic/app.coffee:20 templat
require './core/template'
res.end util.inspect template
当我用例如res.end“hello world”替换这一行时,我得到以下错误:
$ /Users/Filipe/Desktop/Smoothic/app.coffee:20
template.parse(template.render("head\n title= pageTitle\n script(type='t
^
TypeError: Object #<Object> has no method 'render'
at Object.handler (/Users/Filipe/Desktop/Smoothic/app.coffee:13:27)
at final_dispatch (/Users/Filipe/Desktop/Smoothic/node_modules/node-simple-router/lib/router.js:275:26)
at Server.dispatch (/Users/Filipe/Desktop/Smoothic/node_modules/node-simple-router/lib/router.js:326:14)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:527:27)
./core/template.coffee:
path = require 'path'
jsdom = require 'jsdom'
class Template
constructor: (file) ->
parse render file
render: (file) ->
content = switch path.extname file
when '.html' then fs.readFileSync file
when '.jade' then jade.renderFile file
parse: (content) ->
jsdom.env
html: content
scripts: ["http://code.jquery.com/jquery.js"]
done: (errors, window) =>
$ = window.$
我不熟悉coffeescript的隐藏功能,但您需要在Template.coffee文件的末尾执行exports=模板,因为它是或者在Template.coffee中执行module.exports=类模板,并使用
Template = require "./template"
AppTemplate = new Template()
或者像这样在导出时实例化类
module.exports = new class PluginManager
比你,我忘了做那件事。但这并不能完全解决问题。错误是模板仍然没有方法“render”,当我打印出“Template=require./core/Template”的值时,“[method:Template]”是未定义的try Template=new require./core/Template”
module.exports = new class PluginManager