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

var http = require("http");
var fs = require("fs");
var url = require("url");

// ์„œ๋ฒ„ ์ƒ์„ฑ
http.createServer(function(request, response) {
  // url ๋’ค์— ์žˆ๋Š” directory/file ์ด๋ฆ„ parsing
  var pathname = url.parse(request.url).pathname;

  console.log("Request for " + pathname + " received!");

  // ํŒŒ์ผ ์ด๋ฆ„์ด ๋น„์–ด์žˆ๋‹ค๋ฉด index.html ๋กœ ์„ค์ •
  if (pathname == "/") {
    pathname = "/index.html";
  }

  // ํŒŒ์ผ ์ฝ๊ธฐ
  fs.readFile(pathname.substr(1), function(error, data) {
    if (error) {
      console.log(error);
      // 1) ํŽ˜์ด์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์„ ๋•Œ
      // HTTP Status: 404 : NOT FOUND
      // Content Type: text/plain
      response.writeHead(404, { "Content-Type": "text/html" });
    } else {
        // 2) ํŽ˜์ด์ง€๋ฅผ ์ฐพ์•˜์„ ๋•Œ
        // HTTP Status: 200 : OK
        // Content Type: text/plain
        response.writeHead(200, {'Content-Type': 'text/html'}); 
    
        // ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ responseBody ์— ์ž‘์„ฑ
        response.write(data.toString());
    }
    // responseBody ์ „์†ก
    
    response.end();

  });
}).listen(8081);

console.log('Server currently running at http://127.0.0.1:8081/ !');
  • client ์—์„œ sever ์ ‘์† ์‹œ URL์—์„œ ์—ด๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ์„ parsing ํ•˜์—ฌ ์—ด์–ด์คŒ

  • file์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด console์— error message ์ถœ๋ ฅ!

Results

Server ์‹คํ–‰

$ node server.js
Server currently running at http://127.0.0.1:8081/ !

http://127.0.0.1:8081/ ์ ‘์†

image-20200328135841386
Request for / received!
Request for /favicon.ico received!
{ [Error: ENOENT: no such file or directory, open 'favicon.ico']
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'favicon.ico' }

http://127.0.0.1:8081/showmeerror ์ ‘์†

image-20200328143323679
Request for /showmeerror received!
{ [Error: ENOENT: no such file or directory, open 'showmeerror']
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'showmeerror' }

http://127.0.0.1:8081/index.html ์ ‘์†

image-20200328144715931
Request for /index.html received!

HTTP Client

ex)

client.js

var http = require('http');

// HTTP Request์˜ option ์„ค์ •ํ•˜๊ธ”
var options = {
    host: 'localhost',
    port: '8081',
    path: '/index.html'
};

// Callback function์œผ๋กœ Response ๋ฐ›์•„์˜ค๊ธฐ
var callback = function(response) {
    // response event๊ฐ€ ๊ฐ์ง€๋˜๋ฉด data๋ฅผ body์— ๋ฐ›์•„์˜ค๊ธฐ
    var  body = '';
    response.on('data', function(data){
        body += data;
    });

    // end event๊ฐ€ ๊ฐ์ง€๋˜๋ฉด data ์ˆ˜์‹ ์„ ์ข…๋ฃŒํ•˜๊ณ  ๋‚ด์šฉ์„ ์ถœ๋ ฅ
    response.on('end', function(){
        // data ์ˆ˜์‹  ์™„๋ฃŒ!
        console.log(body);
    });

    // Server์— HTTP Request ๋‚ ๋ฆฌ๊ธฐ~!
    var request = http.request(options, callback);
    request.end();
}
  • response.on() ์—์„œ .on() method๋ฅผ ํ†ตํ•ด response๊ฐ€ EventEmitter class๋ฅผ ์ƒ์†ํ•œ object ๋ผ๋Š” ๊ฒƒ ์•Œ ์ˆ˜ ์žˆ์Œ!

Results

$ node client.js
<html>
<head>
    <title>HTTP server test</title>
</head>
<body>
    <p> Testing web server coding with http module!</p>
</body>
</html>

Last updated

Was this helpful?