본문 바로가기

Node.js

[2024.01.14] Node.js #1 hello node.js 출력

<< 소스 코드 >>

const http = require("http");   //require를 사용하여 http 모듈을 로딩하여 http 변수할당.
let count = 0;

//http.createServer(callback);는 서버 인스턴스를 만드는 함수
const server = http.createServer((req,res) => {
    log(count); //전역 변수의 로그를 간단하게 남김.
    res.statusCode = 200;   //http 프로토콜에서 200은 성공을 나타냄.
    res.setHeader("Content-Type", "text/plain");    //Content-Type를 text/plain 타입으로 설정
    res.write("hello\n");   //응답으로 메세지 hello를 write.
    setTimeout(() => {
        res.end("Node.js"); //응답 메세지 Node.js를 서버에서 클라이언트로 전송하고 end로 커넥션을 끝냄.
    }, 2000);   //2초 후 실행
});

function log(count){
    console.log((count += 1));
}

server.listen(8000);    //서버 포트를 8000번 지정

 

<< 출력 화면 >>

 

<< k6 테스트 코드 >>

import http from "k6/http";

export const options = {
    vus : 100,  //vus는 visual users를 나타내는 가상 유저임.
    duration : "10s",   //테스트 진행 시간
};

export default function(){
    http.get("http://localhost:8000");  //http.get 메소드로 요청을 보냄.
}

 

<< k6 테스트 결과 >>