Through this example, it will be explained to divide the node.js code into different modules/folders for better undertandibility. Following this technique makes it easier for other developers to understand the code as he can directly refer to concerned file instead of going through whole code. The major use is when you are working in a team and a new developer joins at a later stage, it will get easier for him to gel up with the code itself.
index.js :- This file will manage server connection.
//Import Libraries
var express = require('express'),
session = require('express-session'),
mongoose = require('mongoose'),
request = require('request');
//Import custom modules
var userRoutes = require('./app/routes/userRoutes');
var config = require('./app/config/config');
//Connect to Mongo DB
mongoose.connect(config.getDBString());
//Create a new Express application and Configure it
var app = express();
//Configure Routes
app.use(config.API_PATH, userRoutes());
//Start the server
app.listen(config.PORT);
console.log('Server started at - '+ config.URL+ ":" +config.PORT);
config.js:-This file will manage all the configuration related params which will remain same throughout.
var config = {
VERSION: 1,
BUILD: 1,
URL: 'http://127.0.0.1',
API_PATH : '/api',
PORT : process.env.PORT || 8080,
DB : {
//MongoDB configuration
HOST : 'localhost',
PORT : '27017',
DATABASE : 'db'
},
/*
* Get DB Connection String for connecting to MongoDB database
*/
getDBString : function(){
return 'mongodb://'+ this.DB.HOST +':'+ this.DB.PORT +'/'+ this.DB.DATABASE;
},
/*
* Get the http URL
*/
getHTTPUrl : function(){
return 'http://' + this.URL + ":" + this.PORT;
}
module.exports = config;
user.js:- Model file where schema is defined
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Schema for User
var UserSchema = new Schema({
name: {
type: String,
// required: true
},
email: {
type: String
},
password: {
type: String,
//required: true
},
dob: {
type: Date,
//required: true
},
gender: {
type: String, // Male/Female
// required: true
}
});
//Define the model for User
var User;
if(mongoose.models.User)
User = mongoose.model('User');
else
User = mongoose.model('User', UserSchema);
//Export the User Model
module.exports = User;
userController:- This file contains the function for user signUp
var User = require('../models/user');
var crypto = require('crypto');
//Controller for User
var UserController = {
//Create a User
create: function(req, res){
var repassword = req.body.repassword;
var password = req.body.password;
var userEmail = req.body.email;
//Check if the email address already exists
User.find({"email": userEmail}, function(err, usr){
if(usr.length > 0){
//Email Exists
res.json('Email already exists');
return;
}
else
{
//New Email
//Check for same passwords
if(password != repassword){
res.json('Passwords does not match');
return;
}
//Generate Password hash based on sha1
var shasum = crypto.createHash('sha1');
shasum.update(req.body.password);
var passwordHash = shasum.digest('hex');
//Create User
var user = new User();
user.name = req.body.name;
user.email = req.body.email;
user.password = passwordHash;
user.dob = Date.parse(req.body.dob) || "";
user.gender = req.body.gender;
//Validate the User
user.validate(function(err){
if(err){
res.json(err);
return;
}else{
//Finally save the User
user.save(function(err){
if(err)
{
res.json(err);
return;
}
//Remove Password before sending User details
user.password = undefined;
res.json(user);
return;
});
}
});
}
});
}
}
module.exports = UserController;
userRoutes.js:- This the route for userController
var express = require('express');
var UserController = require('../controllers/userController');
//Routes for User
var UserRoutes = function(app)
{
var router = express.Router();
router.route('/users')
.post(UserController.create);
return router;
}
module.exports = UserRoutes;
The above example may appear too big but if a beginner at node.js with a little blend of express knowledge tries to go through this will find it easy and really helpful.