Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:15:18+00:00 2026-06-06T18:15:18+00:00

I have the following Node.js directory structure. |–app/ |–app.js |–routers/ |–index.js/ |–models/ |–schemas.js |–post.js

  • 0

I have the following Node.js directory structure.

|--app/
   |--app.js
   |--routers/
      |--index.js/
   |--models/
      |--schemas.js
      |--post.js

In app.js, there’s a line likes this mongoose.connect('mongodb://localhost/' + config.DB_NAME);. In schema.js:

var mongoose = require('mongoose')
    , Schema = mongoose.Schema
    , ObjectId = Schema.ObjectId;

var PostSchema = new Schema({
    title: String
    , author: String
    , body: String
    , creataAt: { 
        type: Date
        , default: Date.now
    }
});

// other schemas goes here

module.exports.PostSchema = PostSchema;

In post.js:

var mongoose = require('mongoose')
    , PostSchema = require('./schemas').PostSchema
    , PostModel = mongoose.model('Post', PostSchema);

module.exports = PostModel;

And in index.js, there might be a line likes this: var PostModel = require('../models/post');. All files mentioned above require mongoose. The purpose of schemas.js is for helping programmers to grasp the schema of the database in a single file. However, I wonder if this implement causes redundancy and cause more overhead, for I require mongoose here and there. Should I pass it as an argument around?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:15:20+00:00Added an answer on June 6, 2026 at 6:15 pm

    If you’re only worried about performance, you need not be. As per http://nodejs.org/docs/latest/api/modules.html#modules_caching:

    Caching

    Modules are cached after the first time they are loaded. This means
    (among other things) that every call to require('foo') will get
    exactly the same object returned, if it would resolve to the same
    file.

    Multiple calls to require('foo') may not cause the module code to be
    executed multiple times. This is an important feature. With it,
    “partially done” objects can be returned, thus allowing transitive
    dependencies to be loaded even when they would cause cycles.

    If you want to have a module execute code multiple times, then export
    a function, and call that function.

    So, you’re not actually requiring Mongoose each time; rather, each time after the first, the cached Mongoose module is returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following node(s) which I retrieve in a streamreader. There could be
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =
I have the following express node.js app. It's using the 'redis' npm package. app.get(/test,function(req,res){
I have the following Schemas in Mongoose/Node.js: var ItemsSchema = new Schema({ itemname: String,
I am developing a network protocol In which I have following requirement each node
We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {
I have following xml and I want to fetch the value of node which
On my node.js server I have the following code: //Generate a token var token
I have the following to execute shell commands using Node: function puts( error, stdout,
I have a tree like structure created out of models using ForeignKey linkages. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.