객체 내장 객체

ObjectHttpCookie

HttpCookie는 HTTP 프로토콜로 캡슐화된 쿠키 객체로, 쿠키 획득 및 설정을 위한 다양한 속성을 제공하며, 여러 쿠키의 구성 및 처리도 지원합니다.http.Request그리고http.Response두 개체가 모두 지원하는 중요한 속성

서버 측에서는 전달할 수 있습니다HttpRequest.cookiesHTTP 요청에서 모든 쿠키를 가져옵니다. 여기의 각 쿠키는 HttpCookie 개체이며, 쿠키 관련 정보는 해당 속성이나 메서드를 통해 얻거나 설정할 수 있습니다. 예를 들어, 다음 코드를 통해 클라이언트가 보낸 쿠키에서 해당 속성을 읽을 수 있습니다.

1 2 3 4 5 6 7
const http = require('http'); const server = new http.Server(8080, function(request) { const cookies = request.cookies; const name = cookies.get('name'); request.response.write(`Hello ${name}!`); }); server.start();

여기서 우리는HttpRequest.cookies속성은 요청의 모든 쿠키를 가져옵니다. 그런 다음 cookie.get 메소드를 사용하여 쿠키 이름을 전달하여 쿠키 값을 가져옵니다.

클라이언트에 쿠키를 보내려면 HttpCookie 개체를 통해 새 쿠키를 만들고 이를 HttpReponse.cookies 컬렉션에 추가합니다. 예는 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const http = require('http'); const server = new http.Server(8080, function(request) { const cookies = request.cookies; let name = cookies.get('name'); if (name) { request.response.write(`Hello ${name} again!`); } else { name = 'User'; request.response.cookies.add(new http.Cookie('name', name, { expires: new Date(Date.now() + 900000) })); request.response.write(`Welcome ${name}!`); } }); server.start();

위의 예에서는 먼저 HttpServletRequest.cookies를 얻은 다음 name이라는 쿠키 값을 읽으려고 시도합니다. 쿠키가 존재하는 경우 요청에 대한 응답을 보내고 쿠키 값을 사용하여 클라이언트를 맞이합니다. 쿠키가 존재하지 않으면 새 쿠키 객체를 생성하여 HttpServletResponse.cookies 컬렉션에 추가합니다. 이름과 값이 있는 첫 번째 매개변수는 쿠키 이름과 해당 값을 지정하는 데 사용됩니다. 동시에 만료 속성을 사용하여 쿠키의 만료 시간을 지정하는데, 여기서는 현재 시간에 15분을 더한 값을 만료 시간으로 지정합니다. 마지막으로 반환 값의 내용에 환영 메시지를 추가합니다.

상속관계

건설자

HttpCookie

HttpCookie 생성자, 새로운 HttpCookie 객체를 생성합니다.

1
new HttpCookie(Object opts = {});

호출 매개변수:

  • opts: 객체, 생성된 쿠키의 속성을 지정합니다.

opts가 설정할 수 있는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9
{ "name": "", // specify the name of the cookie "value": "", // specify the value of the cookie "expires": Date, // specify the expires time of the cookie "domain": "", // specify the domain of the cookie "path": "", // specify the path of the cookie "secure": false, // specify the secure of the cookie "httpOnly": false, // specify the httpOnly of the cookie }

HttpCookie 생성자, 새로운 HttpCookie 객체를 생성합니다.

1 2 3
new HttpCookie(String name, String value, Object opts = {});

호출 매개변수:

  • name: 문자열, 생성된 쿠키의 이름을 지정합니다.
  • value: 문자열, 생성된 쿠키 값을 지정합니다.
  • opts: 객체, 생성된 쿠키의 다른 속성을 지정합니다.

opts가 설정할 수 있는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7
{ "expires": Date, // specify the expires time of the cookie "domain": "", // specify the domain of the cookie "path": "", // specify the path of the cookie "secure": false, // specify the secure of the cookie "httpOnly": false, // specify the httpOnly of the cookie }

구성원 속성

name

문자열, 쿼리 및 쿠키 이름 설정

1
String HttpCookie.name;

value

문자열, 쿼리 및 쿠키 값 설정

1
String HttpCookie.value;

domain

쿠키 쿼리 및 설정을 위한 문자열, 도메인 이름 범위

1
String HttpCookie.domain;

path

문자열, 쿼리 및 쿠키 경로 범위 설정

1
String HttpCookie.path;

expires

날짜, 쿼리 및 쿠키 만료 시간 설정

1
Date HttpCookie.expires;

httpOnly

쿠키만 허용되는지 여부를 부울, 쿼리 및 설정http요청, 기본값은 false

1
Boolean HttpCookie.httpOnly;

secure

부울, 쿠키가 https를 통해서만 전달되는지 여부를 쿼리하고 설정합니다. 기본값은 false입니다.

1
Boolean HttpCookie.secure;

멤버 함수

parse

주어진 문자열을 구문 분석하고 쿠키 객체를 채웁니다.

1
HttpCookie.parse(String header);

호출 매개변수:

  • header: 문자열, 구문 분석해야 하는 헤더 문자열을 지정합니다.

match

주어진 것을 테스트하라url현재 설정과 일치합니까?

1
Boolean HttpCookie.match(String url);

호출 매개변수:

  • url: 문자열, 테스트 지정url

결과 반환:

  • Boolean, 일치가 성공하면 true를 반환합니다.

toString

객체의 문자열 표현을 반환하며 일반적으로 "[Native Object]"를 반환하며 객체 자체의 특성에 따라 다시 구현될 수 있습니다.

1
String HttpCookie.toString();

결과 반환:

  • String, 객체의 문자열 표현을 반환합니다.

toJSON

객체의 JSON 형식 표현을 반환하며 일반적으로 객체에 의해 정의된 읽을 수 있는 속성 컬렉션을 반환합니다.

1
Value HttpCookie.toJSON(String key = "");

호출 매개변수:

  • key: 문자열, 사용되지 않음

결과 반환:

  • Value, 직렬화 가능한 JSON을 포함하는 값을 반환합니다.