@jsh/http

Since v8.0.52

request()

간단한 HTTP 클라이언트 요청 헬퍼 함수입니다.

사용 형식
request(url, option)
매개변수
  • url String 요청 대상 주소입니다. 예: http://192.168.0.120/api/members
  • option Object 선택적 ClientRequestOption입니다.
반환값
사용 예시
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const {println} = require("@jsh/process");
const http = require("@jsh/http")
try {
    req = http.request("http://127.0.0.1:29876/hello")
    req.do((rsp) => {
        println("url:", rsp.url);
        println("error:", rsp.error());
        println("status:", rsp.status);
        println("statusText:", rsp.statusText);
        println("body:", rsp.text());
    })
} catch (e) {
    println(e.toString());
}

Client

HTTP 클라이언트 객체입니다.

생성
Constructor설명
new Client()HTTP 클라이언트를 생성합니다.

do()

지정한 URL로 HTTP 요청을 전송하고 응답을 처리합니다. 메서드, 헤더, 본문 등 옵션과 콜백 함수를 함께 전달할 수 있습니다.

사용 형식
client.do(url)
client.do(url, option)
client.do(url, option, callback)
매개변수
반환값
  • Object
PropertyType설명
statusNumberHTTP 상태 코드
statusTextStringHTTP 상태 메시지
urlString요청 URL
errorString오류 메시지
사용 예시
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const http = require("@jsh/http");

const client = new http.Client()
client.do(
    "http://127.0.0.1:29876/hello",
    { method:"GET" }, 
    (rsp)=>{
        println("url:", rsp.url);
        println("error:", rsp.error());
        println("status:", rsp.status);
        println("statusText:", rsp.statusText);
        println("content-type:", rsp.headers["Content-Type"]);
        println("body:", rsp.text());
    })

ClientRequestOption

옵션타입기본값설명
methodStringGETGET, POST, DELETE, PUT 등 HTTP 메서드
headersObject요청 헤더
bodyString전송할 본문
unixString유닉스 도메인 소켓 파일 경로

unix 옵션을 지정하면 해당 소켓 파일로 서버에 연결을 시도합니다.

ClientRequest

do()

ClientRequest 객체에서 콜백을 실행해 응답을 처리합니다.

사용 형식
do(callback)
매개변수
  • callback (response) => {} 응답을 처리할 콜백입니다.
반환값

없음.

ClientResponse

프로퍼티

PropertyType설명
statusNumber상태 코드 (예: 200, 404)
statusTextString상태 메시지 (예: 200 OK)
headersObject응답 헤더
methodString요청 메서드
urlString요청 URL
errorString오류 메시지

text()

응답 본문을 문자열로 반환합니다.

json()

응답 본문을 JSON 객체로 반환합니다.

csv()

응답 본문을 CSV 행 배열로 반환합니다.

Server

HTTP 서버 객체입니다.

사용 예시
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const http = require("@jsh/http")
const svr = new http.Server({
    network:'tcp',
    address:'127.0.0.1:8080',
})
svr.get("/hello/:name", (ctx) => {
    let name = ctx.param("name");
    let hello = ctx.query("greeting");
    hello = hello == "" ?  "hello" : hello;
    ctx.JSON(http.status.OK, {
        greeting: hello,
        name:  name,
    })
})
svr.static("/html", "/html")
svr.serve();
생성
Constructor설명
new Server(options)HTTP 서버를 생성합니다.
옵션
옵션타입기본값설명
networkStringtcptcp, unix
addressStringhost:port, /path/to/file
  • TCP/IP 예시: {network:"tcp", address:"192.168.0.100:8080"}
  • Unix 도메인 소켓 예시: {network:"unix", address:"/tmp/http.sock"}

all()

모든 HTTP 메서드를 한 번에 처리할 라우트를 등록합니다.

사용 형식
all(request_path, handler)
매개변수
  • request_path String 매칭할 URL 경로입니다.
  • handler (context) => {} context를 사용해 요청 정보를 처리하는 콜백입니다.
반환값

없음.

사용 예시
1
2
3
4
5
6
7
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.all("/api/resource", (ctx) => {
    ctx.JSON(http.status.OK, { message: "Handled all methods" });
});
svr.serve();

get()

GET 요청을 처리할 라우트를 등록합니다.

사용 형식
get(request_path, handler)
매개변수
  • request_path String 매칭할 URL 경로입니다.
  • handler (context) => {} 요청을 처리할 콜백입니다.
반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.get("/hello/:name", (ctx) => {
    const name = ctx.param("name");
    ctx.JSON(http.status.OK, { message: `Hello, ${name}!` });
});
svr.serve();

post()

POST 요청을 처리할 라우트를 등록합니다.

사용 형식
post(request_path, handler)
매개변수
  • request_path String 매칭할 URL 경로입니다.
  • handler (context) => {} 요청 본문 등을 처리할 콜백입니다.
반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.post("/submit", (ctx) => {
    const data = ctx.body; // 요청 본문에 접근합니다.
    ctx.JSON(http.status.Created, { message: "Data received", data: data });
});
svr.serve();

put()

PUT 요청을 처리할 라우트를 등록합니다.

사용 형식
put(request_path, handler)
매개변수
  • request_path String
  • handler (context) => {} 요청 정보를 처리할 콜백입니다.
반환값

없음.

delete()

DELETE 요청을 처리할 라우트를 등록합니다.

사용 형식
delete(request_path, handler)
매개변수
  • request_path String
  • handler (context) => {} 요청 정보를 처리할 콜백입니다.
반환값

없음.

static()

정적 디렉터리를 라우트에 매핑합니다. HTML, CSS, JavaScript, 이미지 등 정적 자산 제공에 적합합니다.

사용 형식
static(request_path, dir_path)
매개변수
  • request_path String 요청 경로 접두사입니다.
  • dir_path String 제공할 디렉터리 경로입니다.
반환값

없음.

사용 예시
1
2
3
4
5
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.static("/public", "/path/to/static/files");
svr.serve();

staticFile()

지정한 파일을 특정 경로로 제공하도록 라우트를 등록합니다.

사용 형식
staticFile(request_path, file_path)
매개변수
  • request_path String 매칭할 URL 경로입니다.
  • file_path String 제공할 파일 경로입니다.
반환값

없음.

사용 예시
1
2
3
4
5
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.staticFile("/favicon.ico", "/path/to/favicon.ico");
svr.serve();

loadHTMLGlob()

HTML 템플릿 파일을 일괄 로드합니다.

사용 형식
loadHTMLGlob(pattern)
매개변수
  • pattern String 글롭 패턴입니다.
반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.loadHTMLGlob("/templates/*.html")
svr.get("/docs/hello.html", ctx => {
    ctx.HTML(http.status.OK, "hello.html", {str:"Hello World", num: 123, bool: true})
})
svr.serve();

serve()

서버를 시작하고 stop()이 호출될 때까지 대기합니다.

사용 형식
serve()
serve(callback)
매개변수
  • callback (result)=>{} ServerResult를 인자로 받는 선택적 콜백입니다.
반환값

없음.

사용 예시
1
2
3
4
5
6
const http = require("@jsh/http");

const svr = new http.Server({ network: 'tcp', address: '127.0.0.1:8080' });
svr.serve((result) => {
    console.log(`Server is listening on ${result.network}://${result.message}`);
});

close()

서버를 중지하고 종료합니다.

사용 형식
close()
매개변수

없음.

반환값

없음.

ServerResult

프로퍼티
프로퍼티타입설명
networkString예: tcp
messageString예: 127.0.0.1:8080

ServerContext

프로퍼티
프로퍼티타입설명
requestObjectServerRequest

abort()

요청 처리를 즉시 중단합니다.

사용 형식
abort()
매개변수

없음.

반환값

없음.

redirect()

지정한 상태 코드로 다른 URL에 리다이렉트합니다.

사용 형식
redirect(statusCode, url)
  • statusCode Number HTTP 상태 코드 (예: 302, http.status.Found)
  • url String 이동할 주소
반환값

없음.

setHeader()

응답 헤더를 설정합니다.

사용 형식
setHeader(name, value)
매개변수
  • name String
  • value String
반환값

없음.

param()

라우트 경로 파라미터 값을 반환합니다.

사용 형식
param(name)
  • name String 파라미터 이름
반환값
  • String 파라미터 값

query()

쿼리 문자열 값을 반환합니다.

사용 형식
query(name)
  • name String 쿼리 키
반환값
  • String 쿼리 값

TEXT()

텍스트 응답을 전송합니다.

사용 형식
TEXT(statusCode, content)
매개변수

없음.

반환값

없음.

사용 예시
1
2
3
4
5
6
7
svr.get("/formats/text", ctx => {
    ctx.TEXT(http.status.OK, "Hello World");
})

// Content-Type: "text/plain; charset=utf-8"
//
// Hello World
1
2
3
4
5
6
7
8
9
svr.get("/formats/text", ctx => {
    name = "PI";
    pi = 3.1415;
    ctx.TEXT(http.status.OK, "Hello %s, %3.2f", name, pi);
})

// Content-Type: "text/plain; charset=utf-8"
//
// Hello PI, 3.14

JSON()

JSON 응답을 전송합니다.

사용 형식
JSON(statusCode, content)
매개변수

없음.

반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
svr.get("/formats/json", ctx => {
    obj = {str:"Hello World", num: 123, bool: true};
    ctx.JSON(http.status.OK, obj);
})

// Content-Type: application/json; charset=utf-8
//
// {"bool":true,"num":123,"str":"Hello World"}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
svr.get("/formats/json-indent", ctx => {
    obj = {str:"Hello World", num: 123, bool: true};
    ctx.JSON(http.status.OK, obj, {indent: true})
})

// Content-Type: application/json; charset=utf-8
//
// {
//     "bool": true,
//     "num": 123,
//     "str": "Hello World"
// }

YAML()

YAML 응답을 전송합니다.

사용 형식
YAML(statusCode, content)
매개변수

없음.

반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
9
svr.get("/formats/yaml", ctx => {
    ctx.YAML(http.status.OK, {str:"Hello World", num: 123, bool: true})
})

// Content-Type: application/yaml; charset=utf-8
//
// bool: true
// num: 123
// str: Hello World

TOML

TOML 응답을 전송합니다.

사용 형식
TOML(statusCode, content)
매개변수

없음.

반환값

없음.

사용 예시
1
2
3
4
5
6
7
8
9
svr.get("/formats/toml", ctx => {
    ctx.TOML(http.status.OK, {str:"Hello World", num: 123, bool: true})
})

// Content-Type: application/toml; charset=utf-8
//
// bool = true
// num = 123
// str = 'Hello World'

XML()

XML 응답을 전송합니다.

사용 형식
XML(statusCode, content)
매개변수

없음.

반환값

없음.

사용 예시
1
2
3
4
5
6
7
svr.get("/formats/xml", ctx => {
    ctx.XML(http.status.OK, {str:"Hello World", num: 123, bool: true})
})

// Content-Type: application/xml; charset=utf-8
//
// <map><str>Hello World</str><num>123</num><bool>true</bool></map>

HTML()

HTML 템플릿을 렌더링해 응답합니다.

사용 형식
HTML(statusCode, template, obj)
  • statusCode Number HTTP 응답 코드
  • template String 템플릿 이름
  • obj any 템플릿에 바인딩할 값
반환값

없음.

사용 예시
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
svr.loadHTMLGlob("/templates/*.html")

svr.get("/hello.html", ctx => {
    obj = {str:"World", num: 123, bool: true};
    ctx.HTML(http.status.OK, "hello.html", obj);
})

// Content-Type: text/html; charset=utf-8
//
// <html>
//     <body>
//         <h1>Hello, World!</h1>
//         <p>num: 123</p>
//         <p>bool: true</p>
//     </body>
// </html>
  • /templates/hello.html
<html>
    <body>
        <h1>Hello, {{.str}}!</h1>
        <p>num: {{.num}}</p>
        <p>bool: {{.bool}}</p>
    </body>
</html>

ServerRequest

프로퍼티
PropertyType설명
methodString요청 메서드
hostString요청 호스트
pathString요청 경로
queryString쿼리 문자열
headerObject요청 헤더
bodyObject요청 본문
remoteAddressString원격 주소

getHeader()

사용 형식
getHeader(name)
매개변수
  • name String head name. e.g. Content-Type, Content-Length
반환값
  • String 헤더 값.

status

HTTP 상태 코드를 제공합니다.

const http = require("@jsh/http");

http.status.OK =                              200;
http.status.Created =                         201;
http.status.Accepted =                        202;
http.status.NonAuthoritativeInfo =            203;
http.status.NoContent =                       204;
http.status.ResetContent =                    205;
http.status.PartialContent =                  206;
http.status.MultipleChoices =                 300;
http.status.MovedPermanently =                301;
http.status.Found =                           302;
http.status.SeeOther =                        303;
http.status.NotModified =                     304;
http.status.UseProxy =                        305;
http.status.TemporaryRedirect =               307;
http.status.PermanentRedirect =               308;
http.status.BadRequest =                      400;
http.status.Unauthorized =                    401;
http.status.PaymentRequired =                 402;
http.status.Forbidden =                       403;
http.status.NotFound =                        404;
http.status.MethodNotAllowed =                405;
http.status.NotAcceptable =                   406;
http.status.ProxyAuthRequired =               407;
http.status.RequestTimeout =                  408;
http.status.Conflict =                        409;
http.status.Gone =                            410;
http.status.LengthRequired =                  411;
http.status.PreconditionFailed =              412;
http.status.RequestEntityTooLarge =           413;
http.status.RequestURITooLong =               414;
http.status.UnsupportedMediaType =            415;
http.status.RequestedRangeNotSatisfiable =    416;
http.status.ExpectationFailed =               417;
http.status.Teapot =                          418;
http.status.UnprocessableEntity =             422;
http.status.Locked =                          423;
http.status.FailedDependency =                424;
http.status.TooEarly =                        425;
http.status.UpgradeRequired =                 426;
http.status.PreconditionRequired =            428;
http.status.TooManyRequests =                 429;
http.status.RequestHeaderFieldsTooLarge =     431;
http.status.UnavailableForLegalReasons =      451;
http.status.InternalServerError =             500;
http.status.NotImplemented =                  501;
http.status.BadGateway =                      502;
http.status.ServiceUnavailable =              503;
http.status.GatewayTimeout =                  504;
http.status.HTTPVersionNotSupported =         505;
http.status.VariantAlsoNegotiates =           506;
http.status.InsufficientStorage =             507;
http.status.LoopDetected =                    508;
http.status.NotExtended =                     510;
http.status.NetworkAuthenticationRequired =   511;
최근 업데이트