Open In App

How to build Password Generator using Node.js ?

Last Updated : 03 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

This article will teach us to create a Password Generator using Node.js.

Approach: For creating this application, we will set up a string of letters, numbers, and characters, then we will fetch 12-digit characters from that string on Random Order using Math.floor() and Math.random() function, then shows the output on the browser. 

Implementation: The application of the aforementioned strategy is shown below.

Step 1: Project Setup:

Initializes NPM: Create and Locate your project folder into the terminal & type the command

npm init -y

It initializes our node application & makes a package.json file.

Create server file: Inside the project folder, create a file  ‘app.js’ using the following command:

touch app.js 

This file contains the Node.js code for creating a password generator.

Step 2: Require HTTP Module: We have to require this module to be a constant, which is then used to call the function of the HTTP module.

const http = require('http');

Step 3: Setup Password Generator: Create a function that returns a random string containing characters, numbers, and letters. This string is created randomly using the following algorithm.

function generatePassword() {
   var length = 12,
       charset = 
"@#$&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@#$&*0123456789abcdefghijklmnopqrstuvwxyz",
       password = "";
   for (var i = 0, n = charset.length; i < length; ++i) {
       password += charset.charAt(Math.floor(Math.random() * n));
   }
   return password;
}

Step 4: Now, use HTTP’s “createServer” method, inside this method we will call the “generatePassword” method with a form that reloads the page on click so that a new password will be generated.

const server = http.createServer((req, res) => {
       res.end(`
       <!doctype html>
       <html>
       <body>
           <h1> ${generatePassword()} </h1>
           <form action="/">
               <button>Generate New Password</button>
           </form>
       </body>
       </html>
     `);
});

Step 5: Setup Listener: We have to set up a listener, which runs the application on port  3000.

server.listen(3000, () => {
   console.log("lishing on http://localhost:3000");
});

Step to run the application: Inside the terminal type the  following command

node app.js

Complete Code:

Javascript




const http = require('http');
  
function generatePassword() {
    var length = 12,
        charset = 
"@#$&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@#$&*0123456789abcdefghijklmnopqrstuvwxyz",
        password = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        password += charset.charAt(Math.floor(Math.random() * n));
    }
    return password;
}
  
const server = http.createServer((req, res) => {
        res.end(`
        <!doctype html>
        <html>
        <body>
            <h1> ${generatePassword()} </h1>
            <form action="/">
                <button>Generate New Password</button>
            </form>
        </body>
        </html>
      `);
});
  
server.listen(3000, () => {
    console.log("lishing on http://localhost:3000");
});


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads