Thursday, November 19, 2020

create your custom server using nodejs

 In nodejs we donot need any 3rd party software to create a server.

1. HTTP module is required to create a server. So first we will include this.

2. Create a  object for server by using the HTTP  createServer method. 

3. Pass req, res as the parameters of this obj.  

NOTE: Use the ES6 arrow function to reduce the function length

4. Send a response('hello from the server side.') from the server-side.

5. Make the sever listen at a port say 3000.

6. On the sever, show that the server is listening. 

NOTE: use nodemon to check if the server has been created

Labels: ,

Wednesday, October 7, 2020

Login Page with express MySQL and nodeJS code

NodeJS , Express and MySQL login page



<!DOCTYPE html>

<html>

<head><title>NodeJS and SQL Login Form</title>

  <link rel="stylesheet" href="style.css">

  </head>

<body>

  

  <div class="login-form">

  <h1>NodeJS and SQL login form</h1>

    <form action="auth" and method ="post">

    <input type="text" name="username" placeholder="required">

      <input type="password" name="password" placeholder="required">

      <input type="submit">

      

   </form>


  

  </div>

 

  </body>

  <script src="Node.js">

    

//include the packages you want to use by creating a variable

    var mysql= require('mysql');

    var express=require('express');

    var session = require('express-session');

    var bodyParser= require('body-parser');

    var path = require('path');

    

    //then connect with the database by creating another variable

    

    var connection = mysql.createConnection({

     host : 'localhost',

     user : 'root',

     database : 'nodelogin',

      password : ''

      

      

    });

    

    

    //Now its time to use express to handle the sessions and http requests

  var app= express();

  

    //its time to use SESSION and BODYPARSER express packages of express

    //the session package will determine if the user is logged in or not

app.use(session({

  secret : 'secret',

  resave: true,

  saveUninitialized: true

  

  

})) ;   

    

    //now use bodyparser package

    //the bodyparser package will extract the data from the login form and then it will parse into json

app.use(bodyParser.urlencoded({extended:true}));

    app.use(bodyParser.json());

         

   

 //make get request:  to request data from a specified source

    //ALERT: Dont use get method while dealing with sensitive data

    //get requests have length restrictions

    //get method can be bookmarked, cannot modify data, remain in browser history

    //sendFile funtion/method of express is used to send static files to the client

    //beginners can try response.sendFile('log.html')

    app.get('/', function(request, repsonse)

           { repsonse.sendFile(path.join(__dirname + 'log.hmtl'));

      

      

    });

        

    


app.post('/auth', function(request, response) {

var username = request.body.username;

var password = request.body.password;

if (username && password) {

connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {

if (results.length > 0) {

request.session.loggedin = true;

request.session.username = username;

response.redirect('/home');

} else {

response.send('Incorrect Username and/or Password!');

}

response.end();

});

} else {

response.send('Please enter Username and Password!');

response.end();

}

});

    

    

    

    

    //make post reqest: to send data to the server to create / update a resource

    //post req have no restrictions on data length

    

    //The data sent to the sever with POST is stored in the request body of HTTP  request

    

    //when the user enters his details, the details are sent to the node server and then our script 

    // will check if such person exists in our database

    

    

    app.post('/auth', function(request, response){

      var username= request.body.username;

      var password= request.body.password;

      if ( username && password) {

        connection.query('SELECT * FROM users WHERE username=? AND password=?',[username,password],

                         function(error, results, fields){

          if (results.length >0){

            request.session.loggedin= true,

              request.session.username= username,

              response.redirect= ('/home')

          } else {

            response.send('please enter username and password');

            response.end();} }

                        

                        

                        )}

      

  

    });



 

    //get request to redirect the user to the home page

    app.get('/', function(request, response){

    if (request.sesion.loggedin){

      response.send('Welcome back,'  + request.session.username + '!');

      

    }  

      response.end();

    }  );

    

    

    //Our web application needs to listen on a port, for testing purposes we'll use port 3000:

    app.listen(3000);

    

    

  

  

  

  </script>

  

</html>

Labels: ,

Must read this about Nodejs

When I was new to web development, I assumed that React Javascript is used for backend purposes but as I continued my journey I found that react.js is for frontend, and for backend we use node.js. 



I think many got the same misconception that reactjs is used for the backend but this is not like that. 


React js is a component-based library or you can say that it is a frontend purpose library to develop interactive user interface. This points to the fact that it is used for the frontend/client-side programming purpose.


It is used to create SPA single-page applications that do not reload and are really fast.


In reactJS, everything is a component and you can change the components at any point in time without affecting the rest of the application.


But NodeJS is a javascript runtime developed by google. It runs javascript code outside the browser whereas React/Vue/Angular are in browser Javascript frameworks.


For a fully functional app, you would use ReactJS to develop its user interface and NodeJS for the backend like for creating an API. 

 Later we will learn about Angular and Vue also













Labels: ,

Tuesday, July 21, 2020

BOOTSTRAP 4 FOR BEGINNERS




 BOOTSTRAP SET UP ALERT:  Put this in the head section of HTML PAGE to use BOOTSTRAP 4





 <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

BOOTSTRAP CONTAINER

<div class="container-fluid  bg-primary text-white">
  <h1>My First Bootstrap Page</h1>
  <p>This is some text.</p>
</div> 


BOOTSTRAP GRID




1. Responsive Column


<div class="row  bg-secondary text-white">
  <div class="col">col1</div>
  <div class="col">col2</div>
  <div class="col">col3</div>

</div> 


2. Different width column


 <div class="row text-white bg-primary">
  <div class="col-sm-8">.col4</div>
  <div class="col-sm-4">.col5</div>
</div>
   

3. Bootstrap bg color

bg-success
bg-info
bg-warning
bg-danger
bg-dark
bg-light


4. Bootstrap text color



 <p class="text-primary">BLUE text.</p>
  <p class="text-success">GREEN text indicates success.</p>
  <p class="text-info">BLUE text represents some information.</p>
  <p class="text-warning">YELLOW text represents a warning.</p>
  <p class="text-danger">RED text represents danger.</p>
  <p class="text-secondary">GREY text.</p>
  

  <p class="text-body">DARK BLACK color (often black).</p>


5. BORDERED TABLE 



<table class="table table-bordered">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>

  </table>



6. Hover row option



<table class="table table-hover ">


7. Bootstrap Images




Responsive images




   <img src="d2.jfif"  class="img-fluid"  alt="donuts">

Centered image
<img src="d2.jfif"  class="mx-auto d-block" alt="donuts">

Float left and float right images
    <img src="d2.jfif"  class="float-right" alt="donuts">
    <img src="d2.jfif"  class="float-left" alt="donuts">

Rounded Corner image 
<img src="d2.jfif"  class="rounded " alt="donuts">


Circular Image
<img src="d2.jfif"  class="rounded-circle " alt="donuts">


Thumbnail image
<img src="d2.jfif"  class="img-thumbnail " alt="donuts">


8. Bootstrap Jumbotron


 ALERT: A jumbotron indicates a big grey box for calling extra attention to some special content or information.


   <div class="jumbotron jumbotron-fluid">
  <div class="container">
    <h1>Bootstrap Tutorial</h1>
    <p>Bootstrap is the most popular frontend framework</p>
  </div>
</div>


9. Bootstrap Alert



<div class="alert alert-success alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Success!</strong>
  </div>



10. Bootstrap button


LINK BUTTON
  <a href="#" class="btn btn-info" role="button">Link Button</a>



BORDER BUTTON
 <a href="#" class="btn btn-outline-primary" role="button">Link Button</a> 

SPINNER BUTTON - LOADING BUTTON
 <button class="btn btn-danger">
    <span class="spinner-border spinner-border-sm"></span>
    Loading..
  </button>

DROPDOWN BUTTON-
<div class="btn-group">
  <button type="button" class="btn btn-primary">Sony</button>
  <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
    <span class="caret"></span>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Tablet</a>
    <a class="dropdown-item" href="#">Smartphone</a>
  </div>
</div>

 












Labels: ,