Web Server - rohit120582sharma/Documentation GitHub Wiki

Servers

The servers are the behind-the-scenes of all web applications - where the client-side code/data comes from. It is a computer connected to the internet - a permanent store of code/data.


Web server

Technically, a web server is a program or a daemon (a service running in the background) which accepts HTTP requests on some port (by default, 80), and replies with HTTP response. It is always-on, ready to receive messages over the internet from users requesting code/data and send it back.

Serving just HTML (along with images, JS, CSS) is serving static content. That is, content that does not change from one user/condition to another. In an Internet dominated by dynamic web applications like social networking services, online shopping, banking portals and others, non-static (dynamic) content is generated on the fly and served.

The web server does this by handing the requests to an application handler (like PHP, Ruby, Pythin, NodeJS, ASP.net) that will do the necessary processing and hands the web server the final content that will be served to the client.

It doesn't provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses.

A lot of web servers are available in the market today like Apache, Apache Tomcat, Microsoft's IIS (Internet Information Server), Nginx, Google Web Server, etc.

Sending the right data back requires using multiple features of the computer:

  • Network socket - receive and send back messages over the internet
  • Filesystem - that’s where the html/css/javascript code is stored in files
  • CPU - for cryptography and optimizing hashing passwords
  • Kernel - I/O management

Techniques:

  • Serialize: translating an object into a format that can be stored or transferred. JSON, XML, CSV, and others are popular ways to serialize the data.
  • Deserialize: It is the opposite; converting the format back into an object.
  • Routing: mapping HTTP requests to content whether actual files that exist on the server or not.

Application server

The application server is a server program that provides the business logic for the application. Most application servers have web server as integral part thus capable of doing whatever a web server can. It provides services like security, DI (Dependency Injection), transaction, concurrency, etc.


const http = require('http');
const port = 3000;

function onInBoundHandler(request, response) {
    /**
     * request » incomingData
     * response » outgoingData
     */
    if (request.url === '/api') {
        var obj = { firstName: 'Rohit', lastName: 'Sharma' };
        response.writeHead(200, {'Content-Type':'application/json'});
        response.write(JSON.stringify(obj));
        response.end();
    }
    response.end('Hello world!');
}
function onOutBoundHandler(request, response) {
    response.end('Sorry for the error!');
}

const server = http.createServer();
server.on('request', onInBoundHandler);
server.on('clientError', onOutBoundHandler);
server.listen(port, function() {
    console.log(`Server running on port ${port}`);
});

⚠️ **GitHub.com Fallback** ⚠️