web server

1. HTTP AND BEING A WEB SERVER

MIME type:

A standard for specifying the type of data being sent.

Stands for 'Multipurpose Internet Mail Extensions'.

Eg: application/json, text/html, image/jpeg

2. Build a web server in Node

stop a running code in VS Code: mac OS , ctrl+alt+m

3. Template:

Text designed to be the basis for final text or content after being processed.

There's usually some specific template language, so the template system knows how to replace placeholders with real values.

4.API, Endpoint:

API: A set of tools for building a software application. Stands for 'Application Programming Interface'. On the web the tools are usually made available via a set of URLs which accept and send only data via HTTP and TCP/IP.

Endpoint: One url in a web API. Sometimes that endpoint(URL) does multiple thing by making choices based on the HTTP request headers.

5. ROUTING:

Mapping HTTP requests to content.

Whether actual files that exist on the server, or not.

eg:

var http=require('http');
var fs = require('fs');

http.createServer(function(req, res){
    if(req.url==='/'){
        fs.createReadStream(__dirname+'/index.html').pipe(res);
    }else if(req.url==='/api'){
        res.writenHead(200, {'Content-Type': 'application/json'});
        var obj = {
            firstname: 'John';
            lastname:'Doe'
        };
        res.end(JSON.stringify(obj));
    }else{
        res.writeHead(404);
        res.end();
    }
}).listen(1337, '127.0.0.1');

Last updated

Was this helpful?