Suresh Mainali
Suresh Mainali

Reputation: 232

Requiring mongoose in node

I am concerned that-

when i require mongoose in one module and configure in same, and when i create schema and module in another module just adding var mongoose = require('mongoose') and work in mongoose with same configuration, how is that possible?? it works though. But i am confused how it worked. doesn't that mean different instance of mongoose???

I tried to do var express = require('express'); var app = express();

in different page and tried to run but it encounters error, i.e configuration in not same, why is that, is't this same as requiring mongoose in two different module and using it.

Upvotes: 2

Views: 121

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

Because of node.js's module caching, every call to require('mongoose') will return the same object.

The behavior you're seeing with that express code is because you're calling express() as a function which creates a separate express app each time it's called.

Upvotes: 2

Related Questions