NodeJS_Unit_4

Web Server

• A web server is software and hardware that uses HTTP (Hypertext Transfer Protocol) and other protocols to respond to client requests made over the World Wide Web.
• The main job of a web server is to display website content through storing, processing and delivering webpages to users.
• Besides HTTP, web servers also support SMTP (Simple Mail Transfer Protocol) and FTP (File Transfer Protocol), used for email, file transfer and storage.
• Web server hardware is connected to the internet and allows data to be exchanged with other connected devices, while web server software controls how a user accesses hosted files.
• The web server process is an example of the client/server model. All computers that host websites must have web server software.

To publish a website, you need either a static or a dynamic web server.
• A static web server, or stack, consists of a computer (hardware) with an HTTP server (software). We call it "static" because the server sends its hosted files as it is to your browser.
• A dynamic web server consists of a static web server plus extra software, most commonly an application server and a database. We call it "dynamic" because the application server updates the hosted files before sending content to your browser via the HTTP server.

Static Website Dynamic Website
Content of Web pages can not be change at runtime. Content of Web pages can be changed.
No interaction with database possible. Interaction with database is possible
It is faster to load as compared to dynamic website. It is slower then static website.
Cheaper Development costs. More Development costs.
HTML, CSS, Javascript is used for developing the website. Server side languages such as PHP, Node.js are used.




Features of HTTP:

o Connectionless protocol: HTTP is a connectionless protocol. HTTP client initiates a request and waits for a response from the server. When the server receives the request, the server processes the request and sends back the response to the HTTP client after which the client disconnects the connection. The connection between client and server exist only during the current request and response time only.
o Media independent: HTTP protocol is a media independent as data can be sent as long as both the client and server know how to handle the data content. It is required for both the client and server to specify the content type in MIME-type header.
o Stateless: HTTP is a stateless protocol as both the client and server know each other only during the current request. Due to this nature of the protocol, both the client and server do not retain the information between various requests of the web pages.
o HTTP is Extensible: It can be integrated with new functionality by providing a simple agreement a client and a server.

4.1 Creating web server

There are mainly two ways as follows:
1. Using http inbuilt module
2. Using require() method
• Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).
• To include the HTTP module, use the require() method:
var http = require('http');
• The HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.
• Use the createServer() method to create an HTTP server:

    var http = require('http');
    //create a server object:
    http.createServer(
    function (req, res) {
    res.write('Hello World!'); //write a response to the client
    res.end(); //end the response
    }
    ).listen(8080); //the server object listens on port 8080
    http.cretesever().listen()

Add an HTTP Header

If the response from the HTTP server is supposed to be displayed as HTML, you should include an HTTP header with the correct content type:

Example:
    var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('Hello World!');
    res.end();
    }).listen(8080);

4.2 Handling http requests

• The http.createServer() method includes request and response parameters which is supplied by Node.js.
• The request object can be used to get information about the current HTTP request e.g., url, request header, and data. The response object can be used to send a response for a current HTTP request.

E.g.
    var http = require('http'); // Import Node.js core module

    var server = http.createServer(function (req, res)
    {
    //create web server
    if (req.url == '/') { //check the URL of the current request
    // set response header
    res.writeHead(200, { 'Content-Type': 'text/html' });
    // set response content
    res.write('This is home age.');
    res.end();
    }

    else if (req.url == "/student")
    {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('This is student Page.');
    res.end();
    }
    else if (req.url == "/admin")
    {

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('This is admin Page.');
    res.end();
    }
    else
    res.end('Invalid Request!');

    });
    server.listen(5000); //listen for any incoming requests
    console.log('Node.js web server at port 5000 is running..')



4.3 Sending requests

The req parameter of the http.createServer method has all the information about the incoming request for eg. request payload, headers, URL etc.
The req parameter has the following properties
request.headers: Information about the request headers such as Connection, Host, Authorization, etc.
request.method : Information about the incoming request methods such as GET, POST, PUT, DELETE, OPTIONS, HEAD, etc.
request.url : Information about the incoming request URL, such as /accounts, /users, /messages etc

No comments:

Post a Comment