MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Good coding style

Basic program for signup


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 they can directly refer to the concerned file instead of going through the 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.

Conclusion

In this page (written and validated by ) you learned about NodeJS Good coding style . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Restful API Design Best Practices.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.