0

运行 node.js 应用程序时出现以下类型错误

/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:5
  application = require("./.app/")();
                                  ^
TypeError: object is not a function
    at Object.<anonymous> (/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:5:35)
    at Object.<anonymous> (/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:21:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
DEBUG: Program node server exited with code 8

DEBUG: Starting child process with 'node server'
00:55:26 - compiled src/social.coffee

我的应用程序结构如下:

☺  tree -L 3 .
.
├── Cakefile
├── README.md
├── server.coffee
├── src
│   ├── config
│   │   ├── config.coffee
│   │   ├── errors.coffee
│   │   ├── i18n.coffee
│   │   ├── models.coffee
│   │   ├── passport.coffee
│   │   └── routes.coffee
│   ├── controllers
│   │   ├── api.coffee
│   │   ├── index.coffee
│   │   ├── login.coffee
│   │   ├── map.coffee
│   │   └── user.coffee
│   ├── index.coffee
│   ├── models
│   │   └── user
│   │       └── user.coffee
│   └── utils
│       ├── dbconnect.coffee
│       ├── emailer.coffee
│       ├── i18n.coffee
│       ├── logger.coffee
│       └── passport.coffee
└── views
    ├── 404.blade
    ├── 500.blade
    ├── footer.blade
    ├── forms
    │   ├── federated-form.blade
    │   ├── login-form.blade
    │   └── registration-form.blade
    ├── header.blade
    ├── index.blade
    └── user
        ├── create.blade
        ├── login.blade
        └── user.blade

server.coffee的如下:

application = require("./.app/")()
nowjs = require "now"
port = process.env.PORT or process.env.VMC_APP_PORT or process.env.VCAP_APP_PORT or 3000
server = application.listen(port)
everyone = nowjs.initialize(server)
console.log "Server running at http://127.0.0.1: "+ port  + "\nPress CTRL-C to stop server."

编译为:

// Generated by CoffeeScript 1.6.2
(function() {
  var application, everyone, nowjs, port, server;

  application = require("./.app/")();

  nowjs = require("now");

  if (!process.env.NODE_ENV) {
    process.env.NODE_ENV = "local";
  }

  port = process.env.PORT || process.env.VMC_APP_PORT || process.env.VCAP_APP_PORT || 3000;

  server = application.listen(port);

  everyone = nowjs.initialize(server);

  console.log("Server running at http://127.0.0.1: " + port + "\nPress CTRL-C to stop server.");

}).call(this);

/app/index.coffee

#Load external dependencies
express = require("express")
stylus = require("stylus")
mongoose = require("mongoose")
i18next = require "i18next"

#Load local dependencies
models = require("./config/models")
i18n = require("./config/i18n")
config = require("./config/config")
routes = require("./config/routes")

#Load database dependencies
dbconnection = require "./utils/dbconnect"

#Load logger
logger = require "./utils/logger"

# Initialize logger
logger.configure()
logCategory = "APP config"

#  Create Server
app = express()
logger.info "---- App server created ----", logCategory

#Exports
module.exports = ->
  #  Load Mongoose Models
  models app

  # Load i18next config
  i18n app
  # Init i18next
  i18next.init(app.i18n)
  i18next.registerAppHelper(app)
  #  Load Expressjs config
  config app

  #  Load routes config
  routes app

  app

logger.info "---- Modules loaded into namespace ----", logCategory
# Connect to database
dbconnection.init (result) ->
  if result
    logger.info "Database initialized", logCategory

所以,每次我运行应用程序时,我都会收到 TypeError,但应用程序会运行!

我在这里想念什么,不明白什么?

非常感谢任何建议。

4

1 回答 1

0

我有一个猜测 - 也许这是编译咖啡脚本和运行 js 之间的某种竞争条件?

无论哪种方式,如果我是您,我会尝试隔离问题-以交互模式打开节点(或咖啡)-需要您的应用程序代码-您可以调用该函数吗?它是一个功能吗?目的?尝试使用咖啡而不是节点来运行您的应用程序。你得到同样的错误吗?

另一件事-您说它在 ./app 中,但在您的代码中它在 ./.app 中-我想这不是问题,因为会发生不同的错误。

于 2013-05-18T11:58:39.303 回答