javascript - include external .js file in node.js app -


i have app.js node application. file starting grow, move part of code in other files "require" or "include" in app.js file.

i'm trying things like:

// declare application var app = require('express').createserver();  // declare usefull stuff db purposes var mongoose = require('mongoose'); var schema = mongoose.schema; var objectid = schema.objectid;  // following require not work require('./models/car.js'); 

in car.js:

// define car model carschema = new schema({   brand        : string,   type : string }); mongoose.model('car', carschema); 

i got error:

referenceerror: schema not defined 

i'm looking have content of car.js loaded (instead of having in same app.js file) there particuliar way in node.js ?

to place emphasis on else has been saying var foo in top level not create global variable. if want global variable write global.foo. all know globals evil.

if uses globals in node.js project on refactor them away there few use cases (there few exceptions isn't one).

// declare application var app = require('express').createserver();  // declare usefull stuff db purposes var mongoose = require('mongoose'); var schema = mongoose.schema; var objectid = schema.objectid;  require('./models/car.js').make(schema, mongoose); 

in car.js

function make(schema, mongoose) {     // define car model     carschema = new schema({       brand        : string,       type : string     });     mongoose.model('car', carschema); }  module.exports.make = make; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -