Creating modules in Node.js
We've actually already used several Node.js modules and created some of our own. Let's look again at our application from Chapter 2, Getting Started with Node.js.
The following code is from routes/index.js and routes/users.js
:
module.exports = router;
The following is the code from app.js
:
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users');
Each of our routes (index and users) is a module. They expose their functionality using the built-in module
object, which is defined by Node.js as a variable scoped to each module. In the preceding example, the object provided by each of our route modules is an Express router instance. The app.js...