ObjectHttpCookie
HttpCookie is a cookie object encapsulated by the HTTP protocol. It provides various attributes for obtaining and setting cookies. It also supports the organization and processing of multiple cookies. It ishttp.Requestandhttp.ResponseAn important property supported by both objects
On the server side, you can passHttpRequest.cookiesto get all cookies in the HTTP request. Each cookie here is an HttpCookie object, and cookie-related information can be obtained or set through its properties or methods. For example, we can read the corresponding attributes from the cookie sent by the client through the following code:
1
2
3
4
5
6
7const 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();
Here we useHttpRequest.cookiesProperty gets all cookies in the request. Then use the cookies.get method, passing in the name of the cookie, to get the value of the cookie.
To send a cookie to the client, create a new cookie through the HttpCookie object and add it to the HttpReponse.cookies collection. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17const 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();
In the above example, we first obtain HttpServletRequest.cookies and then try to read the value of the cookie named name from it. If the cookie exists, send a response to the request and use the cookie value to greet the client. If the cookie does not exist, we create a new cookie object and add it to the HttpServletResponse.cookies collection. The first parameter with name and value is used to specify the cookie name and corresponding value. At the same time, the expires attribute is used to specify the expiration time of the cookie. Here, the current time plus 15 minutes is specified as the expiration time. Finally, we add a welcome message to the content of the return value.
inheritance relationship
Constructor
HttpCookie
HttpCookie constructor, creates a new HttpCookie object
1new HttpCookie(Object opts = {});
Call parameters:
- opts: Object, specifies the properties of the created cookie
The options that opts can set are as follows:
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 constructor, creates a new HttpCookie object
1
2
3new HttpCookie(String name,
String value,
Object opts = {});
Call parameters:
- name: String, specifies the name of the cookie created
- value: String, specifies the created cookie value
- opts: Object, specify other attributes of the created cookie
The options that opts can set are as follows:
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
}
member properties
name
String, query and set cookie name
1String HttpCookie.name;
value
String, query and set cookie values
1String HttpCookie.value;
domain
String, domain name range for querying and setting cookies
1String HttpCookie.domain;
path
String, query and set cookie path range
1String HttpCookie.path;
expires
Date, query and set cookie expiration time
1Date HttpCookie.expires;
httpOnly
Boolean, query and set whether cookies are only allowedhttpRequest, default false
1Boolean HttpCookie.httpOnly;
secure
Boolean, query and set whether cookies are only passed through https, default false
1Boolean HttpCookie.secure;
member function
parse
Parse the given string and populate the cookie object
1HttpCookie.parse(String header);
Call parameters:
- header: String, specifies the header string that needs to be parsed
match
Test the givenurlDoes it match the current settings?
1Boolean HttpCookie.match(String url);
Call parameters:
- url: String, specifying the testurl
Return results:
- Boolean, returns true if the match is successful
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String HttpCookie.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value HttpCookie.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable