Nodejs Login and Registration with PHP MySQL and SQLite Example
we have used nodejs Form, CSS and mysql server.This Example Show to you how to User simple login form and registration form using nodejs and mysql with example
User login and registration using nodejs and mysql with example,node.js mysql authentication,node.js – What does body-parser do with express in nodejs,login authentication with node js
Step1: simple Table and dir means directory structure using Nodejs
CREATE TABLE `clients` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `cr_date` datetime NOT NULL, `up_date` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
SETUP THE PROJECT STRUCTURE NODEJS
directory structure ├── js/mycontrollers │ └── authenticate-controller.js │ └── register-controller.js ├── node_libs ├── nodejsconfig.js ├── main.js └── package.json In Node.js simple package.json simple file is used to steps install all the dependencies.
{ "name": "login", "version": "1.0.0", "description": "login authentication", "main": "main.js", "dependencies": { "body-parser": "^1.17.1", "express": "^4.14.1", "jsonwebtoken": "^7.3.0", "mysql": "^2.13.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Step 2: nodejsconfig.js to configure database connectivity using Nodejs
<bnodejsconfig.js
var mysql = require('mysql'); var dbconnect = mysql.createdbconnect({ host : 'localhost', user : 'root', password : '', database : 'test' }); dbconnect.connect(function(err){ if(!err) { alert("your Database is connected"); } else { alert("Error simple while connecting not with database"); } }); module.exports = dbconnect;
Step 3: main.js
var express=require("express"); var parsendata=require('body-parser'); var app = express(); var AuthCtrl=require('./js/mycontrollers/authenticate-controller'); var RegeCtrl=require('./js/mycontrollers/register-controller'); app.use(parsendata.urlencoded({extended:true})); app.use(parsendata.json()); /* route to simple handle simple form login and simple registration */ app.post('/api/register',RegeCtrl.register); app.post('/api/authenticate',AuthCtrl.authenticate); app.listen(8012);
Step 4: Create simple Register Controller using Nodejs
controller/register-controller.js
var dbconnect = require('./../config'); module.exports.register=function(req,res){ var mydate = new Date(); var clients={ "name":req.body.name, "email":req.body.email, "password":req.body.password, "cr_date":mydate, "up_date":mydate } dbconnect.query('INSERT INTO clients SET ?',clients, function (error, response, fields) { if (error) { res.json({ status:false, message:'there are some error with query' }) }else{ res.json({ status:true, data:response, message:'user registered sucessfully' }) } }); }

Step 5: Create Authenticate Controller using Nodejs
js/mycontrollers/authenticate-controller.js
var dbconnect = require('./../config'); module.exports.authenticate=function(req,res){ var email=req.body.email; var password=req.body.password; dbconnect.query('SELECT * FROM clients WHERE email = ?',[email], function (error, response, fields) { if (error) { res.json({ status:false, message:'there are some miner error with query' }) }else{ if(response.length >0){ if(password==response[0].password){ res.json({ status:true, message:'your form successfully good luck authenticated' }) }else{ res.json({ status:false, message:"your Email and password does issue not match error" }); } } else{ res.json({ status:false, message:"your Email does not any exits" }); } } }); }

Before going to simple form with testing with the help of here demo postman or other Advance client rest tools, start server simple from the cmd to command prompt first :
node main.js