【问题标题】:Node js require doesn't work properly节点 js 要求无法正常工作
【发布时间】:2015-03-08 17:12:47
【问题描述】:

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

为了测试包含的内容,我尝试打印出 '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,或者编译器有错误,但这没有任何意义,因为通常所有文件都是在服务器开始运行之前编译的。

我会非常感谢你的帮助

这是我的代码:

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

【问题讨论】:

标签: javascript node.js coffeescript require


【解决方案1】:

要么在你的 template.coffee 中做一个module.exports = class Template 并使用它实例化它

Template = require "./template"
AppTemplate = new Template()

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

module.exports = new class PluginManager

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 比你,我忘了这样做。但这并不能完全解决问题。错误仍然是“模板没有方法'render'”,当我打印出'template = require“./core/template”'的值时,我得到'[method:Template]'未定义
    猜你喜欢
    • 2017-11-08
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多