Node js require 不能正常工作

Node js require doesn't work properly

我正在开发节点应用程序,但节点并不需要我的 类 正确。取而代之的是 returns '{}'.

为了测试包含的内容,我尝试打印出 'require "./core/template"',但它 returns '{}':

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)

所以我唯一的猜测是该文件可能尚未编译为 javascript,或者编译器有一个错误,但这没有任何意义,因为通常所有文件都在编译之前服务器启动 运行.

非常感谢您的帮助

这是我的代码:

app.coffee:

http = require 'http'
util = require 'util'
Router = require 'node-simple-router'
cson = require 'cson'

# I'm trying to include this coffee class
template = require './core/template'

router = new Router()

router.get '/', (req, res) ->
  res.writeHead 200

  # This method is supposed to render jade and parse it to the 
  # client, but thats not the problem
  template.parse template.render """
                                  head
                                    title= pageTitle
                                    script(type='text/javascript').
                                      if (foo) {
                                         bar(1 + 5)
                                      }
                                  body
                                    h1 Jade - node template engine
                                    #container.col
                                      if youAreUsingJade
                                        p You are amazing
                                      else
                                        p Get on it!
                                      p.
                                        Jade is a terse and simple
                                        templating language with a
                                        strong focus on performance
                                        and powerful features.
                                 """

  # In order to test what has been included I try to print out the 
  # 'require "./core/template"', but it returns 'undefined'
  #
  res.end util.inspect template

http.createServer router
  .listen 8080

./core/template.咖啡:

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,因为它是 described in the module doc of node.js

在您的 template.coffee 中执行 module.exports = class Template 并使用

实例化它
Template = require "./template"
AppTemplate = new Template()

或像这样在导出时实例化 class

module.exports = new class PluginManager