Web Module

Web server & web client with http module

HTTP Server

ex)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTTP server test</title>
</head>
<body>
    <p> Testing web server coding with http module!</p>
</body>
</html>

server.js

  • When a client connects to the server, it parses the file to open from the URL and serves it

  • If the file does not exist, an error message is printed to the console!

Results

Run the Server

Access http://127.0.0.1:8081/arrow-up-right

image-20200328135841386

Access http://127.0.0.1:8081/showmeerrorarrow-up-right

image-20200328143323679

Access http://127.0.0.1:8081/index.htmlarrow-up-right

image-20200328144715931

HTTP Client

ex)

client.js

  • From response.on(), the .on() method tells us that response is an object that inherits the EventEmitter class!

Results

Last updated