节点js要求不能正常工作

时间:2015-01-10 13:06:39

标签: javascript node.js coffeescript require

我正在开发节点应用,但节点不需要我的课程。相反,它会返回“{}”。

为了测试已包含的内容,我尝试打印出来 'require“./core/template”',但它返回'{}':

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,或者编译器有错误,但这没有任何意义,因为通常所有文件都是在服务器开始运行之前编译的。 / p>

我会非常感谢你的帮助

这是我的代码:

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

./芯/ 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.$

2 个答案:

答案 0 :(得分:0)

我不熟悉coffeescript的隐藏功能,但您需要在template.coffee文件的末尾执行exports=Template,因为它是described in the module doc of node.js

答案 1 :(得分:0)

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

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

或像这样实例化导出类

module.exports = new class PluginManager