简体   繁体   中英

JavaScript Requires and Exports in Node.JS

I am writing a Node.JS REST API using Express, and I was a little confused about the require() and exports statements in Node.JS.

For instance, let's say I am writing a simple app wherein app.js contains the basic app.get statements, routes.js contains the functions passed as a callback to those app.get statements and events.js contains the Mongoose Schemas and Models.

Now, if routes.js requires events.js , can I call Model.find() and functions like those in routes.js and if yes, what exports will I have to make from events.js ?

No need to export anything from model. Just require it.

In routes.js, you can access your model using mongoose.model('Schema')

routes.js

require('./events.js')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM