Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript需要在Node.JS中进行编译和导出_Node.js_Rest_Express_Mongoose - Fatal编程技术网

JavaScript需要在Node.JS中进行编译和导出

JavaScript需要在Node.JS中进行编译和导出,node.js,rest,express,mongoose,Node.js,Rest,Express,Mongoose,我正在使用Express编写Node.JS REST API,对Node.JS中的require()和exports语句有点困惑 例如,假设我正在编写一个简单的应用程序,app.js包含基本的app.get语句,routes.js包含作为回调传递给那些app.get语句和events的函数。js包含Mongoose模式和模型 现在,如果routes.js需要events.js,我可以调用Model.find()和routes.js中的函数吗?如果是,我需要从events.js导出什么?无需从模型

我正在使用Express编写Node.JS REST API,对Node.JS中的
require()
exports
语句有点困惑

例如,假设我正在编写一个简单的应用程序,
app.js
包含基本的
app.get
语句,
routes.js
包含作为回调传递给那些
app.get
语句和
events的函数。js
包含Mongoose模式和模型


现在,如果
routes.js
需要
events.js
,我可以调用Model.find()和
routes.js
中的函数吗?如果是,我需要从
events.js
导出什么?

无需从模型导出任何内容。只需要它

在routes.js中,您可以使用mongoose.model('Schema')访问您的模型

routes.js

require('./events.js')

var mongoose = require('mongoose')
    , Model = mongoose.model('myModel')