Nodejs + CoffeeScript + Mongoose:定义模块?
我正在尝试创建一个小应用程序来使用nodejs和mongodb存储代码片段 我正在使用 Coffeescript 来编写应用程序。
问题是,我想将模块中的代码分开 所以我创建了这个文件夹结构
/app
/lib
/models
/routes
core.coffee
core.coffe 是使用expressjs的“服务器”应用程序 所以在这个文件中,我有
mongoose = module.exports.mongoose = require 'mongoose'
app = module.exports.app = express.createServer()
Snippet = module.exports.Snippet = require __dirname+'/lib/models/Snippet'
#App configurations
routes = require(__dirname+'/lib/routes/general')
In lib/models/Snippet
mongoose = module.parent.exports.mongoose
Snippet = new mongoose.Schema
title:
type: String
default:'Title'
mongoose.model 'Snippet',Snippet
exports.Snippet = mongoose.model 'Snippet'
In /lib/routes/general.coffee
app = module.parent.exports.app
mongoose = module.parent.exports.mongoose
Snippet = module.parent.exports.Snippet
app.get '/test', (req,res)->
snip = new Snippet()
res.send snip
但这不起作用,我收到以下错误消息
TypeError: object is not a function
at Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
我怎样才能做到这一点?
I'm trying to create a little application to store snippets of code using nodejs and mongodb
I'm using Coffeescript to write the app.
The problem is, i want to separate the code in modules
so i create this folder structure
/app
/lib
/models
/routes
core.coffee
The core.coffe is the "server" app using expressjs
so in this file i have
mongoose = module.exports.mongoose = require 'mongoose'
app = module.exports.app = express.createServer()
Snippet = module.exports.Snippet = require __dirname+'/lib/models/Snippet'
#App configurations
routes = require(__dirname+'/lib/routes/general')
In lib/models/Snippet
mongoose = module.parent.exports.mongoose
Snippet = new mongoose.Schema
title:
type: String
default:'Title'
mongoose.model 'Snippet',Snippet
exports.Snippet = mongoose.model 'Snippet'
In /lib/routes/general.coffee
app = module.parent.exports.app
mongoose = module.parent.exports.mongoose
Snippet = module.parent.exports.Snippet
app.get '/test', (req,res)->
snip = new Snippet()
res.send snip
But this don't work i get the following error message
TypeError: object is not a function
at Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
How can I accomplish that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到一个值得注意的拼写错误:
将
module.exports.Snippt
更改为module.exports.Snippet
。I see a noteworthy typo:
Change
module.exports.Snippt
tomodule.exports.Snippet
.让我们首先看看如何使用 require。看起来您正在尝试将项目的所有需求加载到 core.coffee 中,然后将它们重新导出到其他地方。这是一种奇怪的做法,大多数人只是在每个需要它们的模块中
需要
这些库(至少现在,请参阅我的答案的结尾)。例如,您在 lib/models/Snippet 中需要 mongoose,因此只需在那里需要它:
lib/models/Snippet:
接下来,无需使用 __dirname 来要求相对路径, require 可以很好地应对以
./
开头的路径:我仍然无法让代码干净地工作(我猜我们没有看到完整的代码),但这可能足以让你设置在正确的道路。
最后,如果您想沿着导出主模块上所有内容的路线,我建议您查看 dave-elkan 的图层项目。普通版本不支持 CoffeeScript,但我创建了一个支持 CoffeeScript 的 fork 。
它非常轻量级,几乎不对您的项目结构做出任何假设。基本思想是你给
layers()
你的express app对象和一个目录。图层将扫描该目录并将任何子目录设置为应用程序对象上的图层。在您的情况下,您将传入
rootPath: __dirname + '/lib'
并且您的应用程序对象将获得app.models.Snippet
和app.routes。通用
添加到它上面。这仍然不是我构建它的方式,但你也许能够从那里想出一些符合你风格的东西。Let's start by looking at how you're using require. It looks like you're trying to load all the project's requirements in core.coffee, and then re-export them elsewhere. That's an odd way of doing it, most people just
require
those libraries in each module that needs them (for now at least, see the end of my answer).For example, you need mongoose in lib/models/Snippet, so just require it there:
lib/models/Snippet:
Next, there's no need to use
__dirname
to require a relative path, require copes fine with a path starting with./
:I still wasn't able to get the code to work cleanly (I'm guessing we're not seeing the full code), but it might be enough to set you on the right path.
Finally, if you want to go down the route of exporting everything on the main module can I suggest taking a look at dave-elkan's layers project. The plain version doesn't support coffeescript, but I've created a fork that does.
It's very lightweight, and makes almost no assumptions about your project structure. The basic idea is that you give
layers()
your express app object and a directory. Layers will scan that directory and set up any subdirectories as layers on your app object.In your case you'd pass in a
rootPath: __dirname + '/lib'
and your app object would getapp.models.Snippet
andapp.routes.general
added onto it. That's still not quite how I'd structure it, but you might be able to come up with something that matches your style from there.