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 8732463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:26:44+00:00 2026-06-13T09:26:44+00:00

Starting to learn node.js and backbone.js and am using the TodoMVC example as my

  • 0

Starting to learn node.js and backbone.js and am using the TodoMVC example as my guide. There are a couple parts I am having trouble wrapping my head around. See below.

Here is app.js.

var express = require('express')
  , http = require('http')
  , mongoose = require('mongoose')
  , models = require('./models')
  , routes = require('./routes')
  , app = express();

app.configure(function () {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(require('stylus').middleware({ src: __dirname + '/public' }));
  app.use(express.static(__dirname + '/public'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
});

app.configure('development', function () {
  app.use(express.errorHandler());
});

routes.init(app);


mongoose.connect("127.0.0.1", "todomvc", 27017);

http.createServer(app).listen(3000);

console.log("Express server listening on port 3000");

Heres is ./models:

var mongoose = require('mongoose'),

  TodoSchema = new mongoose.Schema({
    title: { 'type': String, 'default': 'empty todo...' },
    order: { 'type': Number },
    done: { 'type': Boolean, 'default': false }
  });

module.exports = mongoose.model('Todo', TodoSchema);

Andy finally, here is ./routes:

(function (exports) {

  "use strict";

  var mongoose = require('mongoose')
    , crudUtils = require('../utils/crudUtils')
    , Todo = mongoose.model('Todo');

  function index(req, res) {
    res.render('index', { 'title': 'Backbone.js, Node.js, MongoDB Todos' });
  }

  exports.init = function (app) {
    app.get('/', index);
    crudUtils.initRoutesForModel({ 'app': app, 'model': Todo });
  };

}(exports));

So my question is, how is the ‘Todo’ model in mongoose.model(‘Todo’) in the routes module available in this scope? I see that the models module is exporting mongoose.model(‘Todo’, TodoSchema); so I have to believe that is how the routes module has access to it, but I don’t know why. What am I missing? I have a feeling its just not a complete understanding of scope in this situation. Also, I am not sure of the reasoning of having the routes function anonymous.

Many thanks!

  • 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-13T09:26:45+00:00Added an answer on June 13, 2026 at 9:26 am

    This is one of the more confusing things to deal with when starting out in Node and Mongoose.

    When you require('mongoose') for the first time, it creates a singleton instance of Mongoose – the same instance is returned every subsequent time you require it.

    This makes it really easy to work with, but is a bit of ‘magic’ that’s hard to understand at the beginning.

    It means that when you call mongoose.connect("127.0.0.1", "todomvc", 27017); in app.js, it creates a connection that persists with the app.

    It also means that mongoose.model('Todo', TodoSchema); makes the Todo model available in any other scope that calls require('mongoose'), via mongoose.model('Todo'). This could be var’d at the top of another file you require as in the example above, or the moment you need it in the middle of a callback.

    This is how you get the Todo model into your routes.js, and a very good reason to ensure telling Mongoose about your models is one of the first things you do in your application.

    To answer your questions regarding understanding scopes; each file you require effectively has its own scope and doesn’t have access to anything except global objects like process. You have to require everything you want to work with, and can only pass variables in by calling functions or creating classes that are exposed via the exports object.

    So for the actual example above there is no benefit in exporting the model from models.js as it’s not subsequently referenced in app.’s where models.js is required. It’s these lines in routes.js that make the Todo model available:

    var mongoose = require('mongoose')
    , Todo = mongoose.model('Todo'); // returns the Todo model that was registered by models.js
    

    That’s how Todo exists on this line:

    crudUtils.initRoutesForModel({ 'app': app, 'model': Todo });
    

    There’s also no benefit (as far as I know) in wrapping the routes in an anonymous function as this is essentially provided by require.

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

Sidebar

Related Questions

I'm just starting to learn c# and I'm having trouble with arrays. The array
I am just starting learn SQL at work, but I am having trouble with
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
I am just starting to learn Backbone.js. How do i create a Backbone model
I'm currently starting to learn about JavaScript on the server side with node, I've
Just starting to learn some Python and I'm having an issue as stated below:
just starting to learn Dojo, and I am having a heck of a time
I'm starting to learn php and am using a maze generator to test my
So i am starting to learn lisp with emacs and was wondering if there
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read

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.