0

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?

Siddharth
  • 1,146
  • 3
  • 15
  • 28

1 Answers1

0

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')
ashu
  • 1,019
  • 7
  • 9