Andrey showed the source code, but if you are also wondering how to use it, a simple and simple explanation is here ( http://nodejs.org/api/modules.html ).
These were two good examples for me.
//foo.js, multiple methods var circle = require('./circle.js'); console.log( 'The area of a circle of radius 4 is ' + circle.area(4)); //circle.js var PI = Math.PI; exports.area = function (r) { return PI * r * r; }; exports.circumference = function (r) { return 2 * PI * r; }; //bar.js var square = require('./square.js'); var mySquare = square(2); console.log('The area of my square is ' + mySquare.area()); //square.js, single method module.exports = function(width) { return { area: function() { return width * width; } }; }
My favorite pattern
(function (controller) { controller.init = function (app) { app.get("/", function (req, res) { res.render("index", {}); }); }; })(module.exports);
Andy Dec 24 '14 at 8:33 2014-12-24 08:33
source share