Categories: node.js

Create a Registration Form With Node.js & MySQL

A registration form is used to collect data from potential users of your website.

This is necessary, especially if you want these users to subscribe to a certain service in which they would have to login to access.

In this tutorial, we will look at how to create a registration form using Node.js and MySQL.

Let’s begin …

First, we create a MySQL database

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(300) NOT NULL,
  `date_time` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

What we want to do next is to create a package.json file where will install the dependencies that are necessary for this project to work.

//Create a package.json file
npm init --y

We will install other dependencies as follows:

npm install express --save // use express sever
npm install mysql //use mysql module
npm install nodemon //watch for file changes
npm install cryptr //encrypt our password

Next we create an index.html file where we will write our HTML code

<html>  
<body>  
 
<form action="/register-controller" method="POST">  
First Name: <input type="text" name="name">  
   
Email: <input type="text" name="email">
Paswword: <input type="password" name="password">
   
<input type="submit" value="Submit">  
</form>
</body>  
</html>  

Next we create a config.js file where we will make the database connection.

  var mysql      = require('mysql');
  var connection = mysql.createPool({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'test2',

  port: '8080'
});
connection.getConnection(function(err){
if(!err) {
    console.log("Database is connected");
} else {
    console.log(`Error while connecting with database ${err}` );
}
});
module.exports = connection; 

Moving forward, we create a controller file that will insert our collected data into the database.

var Cryptr = require('cryptr');
var express=require("express");
var connection = require('./config');
 cryptr = new Cryptr('myTotalySecretKey');
 
module.exports.register=function(req,res){
    var today = new Date();
  var encryptedString = cryptr.encrypt(req.body.password);
    var users={
        "name":req.body.name,
        "email":req.body.email,
        "password":req.body.password,
        "created_at":today,
        "updated_at":today
    }
    connection.query('INSERT INTO users SET ?',users, function (error, results, fields) {
      //connection.release()
      if (error) {
        res.json({
            status:false,
            message:'there are some error with query'
        })
      }else{
          res.json({
            status:true,
            data:results,
            message:'user registered successfully'
        })
      }
    });
}

Your result should look like this:

Author

Recent Posts

Google Launches Its Own ‘Reasoning’ AI Model to Compete with OpenAI

This month has been packed for Google as it ramps up efforts to outshine OpenAI…

2 days ago

You can now use your phone line to call ChatGPT when cellular data is unavailable.

OpenAI has been rolling out a series of exciting updates and features for ChatGPT, and…

3 days ago

Phishers use fake Google Calendar invites to target victims

A financially motivated phishing campaign has targeted around 300 organizations, with over 4,000 spoofed emails…

4 days ago

Hackers Exploiting Microsoft Teams to Remotely Access Users’ Systems

Hackers are exploiting Microsoft Teams to deceive users into installing remote access tools, granting attackers…

5 days ago

Ethical Hacking Essentials

Data plays an essential role in our lives.  We each consume and produce huge amounts…

7 days ago

Thomas E. Kurtz, co-creator of the BASIC programming language, passes away at 96.

Thomas E. Kurtz, co-creator of the BASIC programming language, passed away on November 12, 2024,…

7 days ago