Setting up HTTP Server Relevel

Setting up HTTP Server

 

HTTP-----Hypertext Transfer Protocol

HTTPS----Hyper Text Transfer Protocol Secure.

 

 

 500 error is  server side error

400 error is user side error

200 show working good.

100 show still requesting and going on and process...

 Create a simple Express server

 
    const express = require('express');
    const app = express();

    const port = 3000;

    app.get('/', (request,response) => {
        response.send("Hello Express :)");
    });


    app.listen(port, ()=> {
        console.log(`Express server listing on port ${port}`);
    })
 

 

 

 Create a simple server

 
    const http = require('http');   // http --> node's core module

    const listener = function(req, res){
        console.log("our server is working.")
        res.writeHead(200);     // 400 is bad request
        res.end("Our server is live")
    }

    const simpleServer = http.createServer(listener);

    simpleServer.listen(8080);




 

 Sending JSON data from the server

//  sending JSON data from the server

    const http = require('http');   // http --> node's core module

    const listener = function(req, res){
        console.log("our server is working.")
        res.setHeader("Content-Type", "text/json");
        res.writeHead(200);     // 400 is bad request
        res.end(`{"message" : "This is a JSON Object from  our server.}`)
    }

    const simpleServer = http.createServer(listener);

    simpleServer.listen(8080);

 

 

 sending CSV data from the server

    //  sending CSV data from the server

    const http = require('http');   // http --> node's core module

    const listener = function(req, res){
        console.log("our server is working.")
        res.setHeader("Content-Disposition", "attachment; filename=myDetails.csv");
        res.writeHead(200);     // 400 is bad request
        res.end(`name, school,age\n Dolby, DPS,21`)
    }

    const simpleServer = http.createServer(listener);

    simpleServer.listen(8080);


 

 

 

 

 

 

 

 

 

 

 

now create your own server. (give it a try amy)

 

 simple server 2 (second class) 


HTTP server (using multiple routes)

 
    const http = require('http');
    const listener = function(req,res){
        res.setHeader("Content-Type",'text/html');
        console.log("Server is working");
        res.writeHead(200);
        // creating multiple routes using switch case  below
        switch(req.url){
            case "/":
                res.end("Http server response: Home Page");
                break;
            case "/1":
                res.end("Http server response: student 1");
                break;          
            case "/2":
                res.end("Http server response: student 2");
                break;        
            case "/name":
                res.end("Http server response: Sanjeet");
                break;
            case "/company":
                res.end("Http server response: MNC");
                break;
            default:
                res.end("<h1>Http server response: Incorrect endpoint.<br><a href = 'http://localhost:3000/name'>Go Here</a></h1>")
        }
    }

    const simpleServer = http.createServer(listener);
    simpleServer.listen(3000);
 
 

 

 

 express.js -- it is a node js frame work.

Basic Express server


    const expressjs = require('express');
    const expressAp = expressjs();

    expressAp.get('/', function (req,res){
        res.send("This is express server");
    })

    expressAp.listen(3000);


 

intermediate Express server (using multiple routes)


    const expressjs = require('express');
    const expressAp = expressjs();

    expressAp.get('/', function (req,res){
        res.send("This is express server");
    });
    expressAp.get('/name', function (req,res){
        res.send("Http server response: Sanjeet");
    });

    expressAp.get('/company', function (req,res){
        res.send("Http server response: MNC");
    });

    expressAp.listen(3000);


    // expressAp.listen(3000, function (){
    //     console.log("server is runing on port 8484.")
    // });


 

 

intermediate Express server 2 (using multiple routes)

 
    const expressjs = require('express');
    const expressAp = expressjs();

    expressAp.get('/', function (req,res){
        res.send("Hello World");
    });
    expressAp.get(/^\/students\/(\d+)$/ , function (req,res){
        res.send("ExpressJs backed http endpoint student id" + req.params[0]);
    });


    // expressAp.get('/students/:studentId', function (req,res){
    //     res.send("ExpressJs backed http endpoint student id " + req.params.studentId);
    // });


    expressAp.get('/students/:studentId/region', function (req,res){
        res.send("ExpressJs backed http endpoint student id " + req.params.studentId + req.params.region);
    });

    // expressAp.get('/students/2', function (req,res){
    //     res.send("ExpressJs backed http endpoint student id 2");
    // });
   
    expressAp.listen(8000, function (){
        console.log("server is running on port 8000.")
    });


 

 Express file Transfer


    const expressjs = require('express');
    const fs = require('fs');
    const path = require('path');
    const expressAp = expressjs();

    expressAp.use(function(req,res,next){
        let filePath = path.join(__dirname,req.url)
        fs.stat(filePath, function(err,fileInfo){
            if(err){
                next();
                return;
            }
            if(fileInfo.isFile()){
                res.sendFile(filePath);
            }
            else{
                next();
            }
        });
    });

    expressAp.listen(3000, function (){
        console.log("server is runing on port 3000.")
    });


 

 Promises are not asynchronous..

 

 Question-> Let us try to create a web server on port 8448 instead of 8080.

Solution :

 

 

 

 

 

 

Question -> Let us try to return a 400 error message! 

Solution :

 

 MCQ

 

  1. If we have to update our password, what HTTP method should we use ?

    1. GET

    2. PUT

    3. POST

    4. UPDATE

       

  2. If there is a Null pointer exception in server code, what response should it return?

    1. 404

    2. 302

    3. 500

    4. 201

       

  3. Which of the following methods has no message body?

    1. POST

    2. GET

    3. PUT

    4. DELETE

       

  4. Find incorrect mapping.

    1. 200 OK

    2. 400 Bad Request

    3. 402 Not Found

    4. 301 Moved Permanently

       

  5. If we have to configure a RESTFull url, for searching a book given its id, what will the request look like.

    1. GET /{id}/books/

    2. GET /{Id}

    3. GET /Books/{id}

    4. GET /Books?id={id1}

 

 

 

 

 

  1.  Express is flexible ______ web application framework .

    1. Node.js

    2. React.js

    3. Angular.js

    4. Golang

  2. Which of the following express.js features .

    1. Allows to create middlewares

    2. AllDefines routing tables

    3. None of these

    4. Both a and b

  3. What function arguments are available to Express Route handlers .

    1. Request Object

    2. Response Object

    3. Next()

    4. All of these

  4. What middleware do we use for custom logging in express.js .

    1. Morgan

    2. Cors

    3. Logger

    4. Log4j

  5. What does [^abc]  regex represent .

    1. Any character without a, b and c

    2. Any character without a, but has b and c

    3. Any character with a, b and c

    4. List of characters without a, b and c

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

إرسال تعليق (0)
أحدث أقدم