Question

it will work well in localhost, when ever i deploy it on to server, registration form doesnot responding??

Posted October 17, 2017 1.4k views
MySQLNode.js

here is my code
var express = require(‘express’),
http = require('http’),
path = require('path’),
hash = require(’./pass’).hash;
var moment = require('moment’);
var bcrypt = require('bcrypt’);
var app = express();
var methodOverride = require('method-override’);
var mysql = require('mysql’);
connection.connect(function(err){
if(!err) {
console.log(“Database is connected … nn”);
} else {
console.log(“Error connecting database … nn”);
}
});
/*
Middlewares and configurations
/
app.configure(function () {
app.use(express.bodyParser());
app.use(express.cookieParser('Authentication Tutorial ’));
app.use(express.session());
app.use(methodOverride(function(req, res){
if (req.body && typeof req.body == 'object’ && ’method’ in req.body)
{
var method = req.body.
method;
delete req.body.method;
return method;
}
}));
app.use(express.static(path.join(
dirname, 'public’)));
app.set('views’, _
dirname + ’/views’);
app.set('view engine’, 'ejs’);
});
var connection = mysql.createConnection({
host : 'hostss’,
user : 'user’,
password : 'password
’,
database : 'db’
});
app.use(function (req, res, next) {
var err = req.session.error,
msg = req.session.success;
delete req.session.error;
delete req.session.success;
res.locals.message = “;
if (err) res.locals.message = ’<p class="msg error”>’ + err + ’</p>’;
if (msg) res.locals.message = ’<p class=“msg success”>’ + msg + ’</p>’;
next();
});
/*
Helper Functions
*/

function user_exits(req, res, next) {
var username= req.body.username;
connection.query(“SELECT * FROM user WHERE username =?”,[username],function (error, results, fields) {
if (error) {
console.log(error);
res.send(“fail internal error”+“\r\n”);
}
if (results.length >0) {
console.log('User is Already Exists’);
res.send(“fail user already exists”+“\r\n”);

} else {

    console.log('No user Available oN that name, U can insert it');
    next();

}

});
}

/*
Routes
*/
app.get(“/”, function (req, res) {

    res.render("index", { moment: moment });

});

app.get(“/signup”, function (req, res) {
res.render(“signup”,{ moment: moment });

});

app.post(“/signup”,user_exits,function (req, res){
bcrypt.hash(req.body.password, 5, function( err, bcryptedPassword) {

var users={
“username”:req.body.username,
“email”:req.body.email,
“password”:bcryptedPassword,

}
console.log(users);
connection.query('INSERT INTO user SET ?’,users, function (error, results, fields) {
if (error) {
console.log(“error ocurred”,error);
res.send({
“code”:400,
“failed”:“error ocurred”
});
}else{
console.log('The solution is: ’, results);
res.send({
“code”:200,
“success”:“user registered sucessfully”
});
}
});
});

});

app.get(“/login”, function (req, res) {
res.render(“login”,{ moment: moment });
});

app.post(“/login”,function (req, res) {
var sess = req.session;
var username= req.body.username;
var password = req.body.password;
connection.query('SELECT * FROM user WHERE username = ?’,[username], function (error, results, fields) {
if (error) {
console.log(error);
}else{
if(results.length >0){
bcrypt.compare(password, results[0].password, function(err, doesMatch){
if (doesMatch){
req.session.userId = results[0].id;
req.session.user = results[0];
console.log(results[0].id);
res.redirect(’/profile/’+results[0].id);
}else{
res.send({
“code”:204,
“success”:“Email and password does not match”
});
}
});
}
else{
console.log(“fine”);
}
}
});
});
but when i upload it on digitalocean server the form is not working?? please developer help me to fix that problem…

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!