Hello world (http)

The http module allows us to perform tasks in relation to the HTTP protocol. The following code snippet showcases how we can use the http module to implement very minimal implantation of a web server:

import * as http from "http"; 
const hostname = "127.0.0.1"; 
const port = 3000; 

const server = http.createServer((req, res) => { 
    res.statusCode = 200; 
    res.setHeader("Content-Types", "text/plain"); 
    res.end("Hello world!"); 
}); 

server.listen(port, hostname, () => { 
    console.log(`Server running at http://${hostname}:${port}/`); 
}); 

We have created a web server that will listen to all the HTTP requests. The http module allows us to implement our web HTTP server, but its level of abstraction is very low. In a real-world application, ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.