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 ...

Get Learning Node.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.