Node.Webサーバをjsにオフセット


Node.js


Google ChromeのJsva Scriptエンジン(V 8 Engine)に基づいて構築されたサーバ側プラットフォームです.
Node.jsは、Webサーバではなくコードを実行する方法です.

特長

  • 非同期I/O処理
  • 快速
  • 単一スレッド
  • 高拡張性
  • MITライセンス
  • よくどこに使いますか。

  • I/O密集型アプリケーション
  • データストリーム管理アプリケーション
  • データをリアルタイムで処理するアプリケーション(例えば、ソケットプログラミング)
  • JSON APIベースのアプリケーション
  • CPU利用率の高いアプリケーションは推奨されません.

    Webサーバのオフセット

    // Node.js에 내장된 라이브러리 
    const http = require("http");
    
    http.createServer(function (req, res){
      // 요청에 응답 헤더를 보내는 ‘http’ 모듈의 내장 속성
      res.writeHead(200);
      // 응답 끝내기. 함수의 return과 비슷하다.
      res.end("Hello World!");
    }).listen(3000, function() {
      console.log("Server on : 3000 port");
    })
    
    createServer()は、第1のパラメータとしてコールバック関数を受け入れる.reqはリクエスト対象(リクエスト)である.resは応答対象(response)である.listen()は、特定のポートを開く関数です.
    アドレスウィンドウにlocalhost:3000と入力すると確認できます.
    const http = require("http");
    
    http.createServer((req, res) => {
      let url = req.url;
      let method = req.method;
      let headers = req.headers;
    
      console.log(url);
      console.log(method);
      console.log(headers);
    
      res.writeHead(200);
      // 한글은 인코딩,,이 필요함
      res.end("hello");
    }).listen(3000, () => {
      console.log("server on : 3000 port");
    });
    
    res.endの文字列がハングルであれば、別途符号化する必要があります.
    server on : 3000 port
    /<- url
    GET <- method
    {
    host: 'localhost:3000',
    connection: 'keep-alive',
    'sec-ch-ua': '"Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
    'sec-ch-ua-platform': '"macOS"',
    accept: 'image/avif,image/webp,image/apng,image/svg+xml,image/,/*;q=0.8',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'no-cors',
    'sec-fetch-dest': 'image',
    referer: ' http://localhost:3000/' ,
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7'
    } <- header