Saturday, 22 April 2017

node.js app is working slower than php

I am working on a node.js application (express + mongoose) that currently has only login and registration. While developing my app I have encountered a problem that node.js is even slower than apache2 + php. The code execution time that I get after creating a new user account on node.js app: http://prntscr.com/ezmh6h and the execution time when I register a user on php website is 178.36401367188ms. Why web request on node.js app is so slow? Have you got any suggestions? Because normally node.js should be 2-3 times faster than php.

My auth.js file:

var express = require('express');
var async = require('async');
var router = express.Router();
var passport = require('passport');

var User = require('../models/user');
var Auth = require('../functions/auth');
require('../configs/passport');

router.get('/', function(req, res) {
    res.redirect('/auth/login');
});

router.get('/login', Auth.ensureNotAuthenticated, function(req, res) {
    res.render('login', {
        title: 'Login'
    });
});

router.post('/login', Auth.ensureNotAuthenticated, passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/auth/login',
    failureFlash: true
}), function(req, res) {
    res.redirect('/');
});

router.get('/register', Auth.ensureNotAuthenticated, function(req, res) {
    res.render('register', {
        title: 'Register'
    });
});

router.post('/register', Auth.ensureNotAuthenticated, function(req, res) {
    var username = req.body.username;
    var email = req.body.email;
    var password = req.body.password;
    var password2 = req.body.password2;

    // Validation
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('password', 'Password is required').notEmpty();
    req.checkBody('password2', 'Passwords don\'t match').equals(password);

    async.waterfall([
        function(callback) {
            req.getValidationResult().then(function(result) {
                if (result.isEmpty()) {
                    var newUser = User({
                        username: username,
                        email: email,
                        password: password
                    });
                    callback(null, newUser);
                } else {
                    callback(result.array()[0].msg);
                }
            });
        },

        function(newUser, callback) {
            User.usernameExists(newUser.username, function(exists) {
                if (exists) {
                    callback('Username already exists');
                } else {
                    callback(null, newUser);
                }
            });
        },

        function(newUser, callback) {
            User.emailExists(newUser.email, function(exists) {
                if (exists) {
                    callback('Email already exists');
                } else {
                    callback(null, newUser);
                }
            });
        },

        function(newUser, callback) {
            User.createUser(newUser, function(err, user) {
                if (err) {
                    callback('An unexpected error has occurred');
                } else {
                    console.log('New user: ' + user.username);

                    req.flash('success_msg', 'You have successfully registered');
                    res.redirect('/auth/login');
                }
            });
        }
    ], function(err) {
        if (err) {
            res.locals.error_msg = err;
        }

        res.render('register', {
            title: 'Register'
        });
    });
});

router.get('/logout', function(req, res) {
    req.logout();
    req.flash('success_msg', 'You have successfully logged out');
    res.redirect('/auth/login');
});

module.exports = router;

User model:

var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');

var Schema = mongoose.Schema({
    username: {type: String},
    email: {type: String},
    password: {type: String}
});
var User = module.exports = mongoose.model('users', Schema);

module.exports.createUser = function(newUser, callback) {
    var salt = bcrypt.genSaltSync(10);
    var hash = bcrypt.hashSync(newUser.password, salt);
    newUser.password = hash;
    newUser.save(callback);
}

module.exports.usernameExists = function(username, callback) {
    User.findOne({username: username}, function(err, result) {
        if (err) {
            callback(true);
        } else if (result) {
            callback(true);
        } else {
            callback(false);
        }
    });
}

module.exports.emailExists = function(email, callback) {
    User.findOne({email: email}, function(err, result) {
        if (err) {
            callback(true);
        } else if (result) {
            callback(true);
        } else {
            callback(false);
        }
    });
}

module.exports.getUserByUsername = function(username, callback) {
    User.findOne({username: username}, callback);
}

module.exports.getUserById = function(id, callback) {
    User.findById(id, callback);
}

module.exports.comparePassword = function(password, hash, callback) {
    bcrypt.compare(password, hash, function(err, res) {
        if (err) {
            callback(null, false);
        }
        callback(null, res);
    });
}



via WaScript3r

No comments:

Post a Comment