Nodejs implements cookie

Source: Internet
Author: User
Tags set cookie

The following code is the code in the initnode framework being written. You may not be able to copy all the codes when implementing cookies.
Cookie implementation mainly refers to sending the header message:
[Javascript]
Res. setHeader ("Set-Cookie", cookieArr );
Res. setHeader ("Set-Cookie", ['username = XXX', 'Age = xxx; path =/foo']); // Note: Multiple cookies must be sent once, multiple calls to this sending header function only take effect for the last time
Complete http setcookie header:
Set-Cookie: customer = huangxp; path =/foo; domain = .ibm.com;
Expires = Wednesday, 19-OCT-05 23:12:40 GMT; [secure]
To implement cookie settings, You need to assemble the above cookie header information. For details, refer to the following code: set_cookie Function
[Javascript]
/*
* InitNode framework Request class
* Core functions
*/
Var request = function (){
 
Var _ this = this;
Var req = null; // request object
Var res = null;
Var postData = null; // stores POST data
Var getData = null; // store GETDATA data
Var cookieData = null; // store cookie data
Var cookieArr = []; // store cookie output data
Var url = require ("url ");
Var util = require ('til ');
Var querystring = require ('querystring ');
Var OS = require ('OS ');
 

/*
* Description: Request Initialization
*/
This. init = function (initnode ){
Req = initnode. req;
Res = initnode. res;
PostData = this. _ post (initnode. postData );
GetData = this. _ get (req );
CookieData = this. _ cookie (req );
};
 
/*
* Description: GET method: GET the GET submitted data in the URL.
* Usage:
* Obtain a single initnode. request. get ('username ');
* Obtain multiple initnode. request. get (['username', 'age']);
* Get all initnode. request. get ('');
*/
This. get = function (name ){
Return this. _ requestData (name, getData );
};
 

/*
* Description: the post method is used to obtain the POST submission data in the URL.
* Usage:
* Obtain a single initnode. request. post ('username ');
* Obtain multiple initnode. request. post (['username', 'age']);
* Get all initnode. request. post ('');
*/
This. post = function (name ){
Return this. _ requestData (name, postData );
};
 
/*
* Description: COOKIE method, used to obtain the COOKIE submission data in the URL.
* Usage:
* Obtain a single initnode. request. get_cookie ('username ');
* Obtain multiple initnode. request. get_cookie (['username', 'age']);
* Get all initnode. request. get_cookie ('');
*/
This. get_cookie = function (name ){
Return this. _ requestData (name, cookieData );
};

/*
* Note: set cookies.
* Usage:
* Set Cookie initnode. request. set_cookie ('username', 'initnode', 30 ,'/');
* Name cookie name
* Value cookie value
* Expires validity period, in seconds
* Path valid directory
* Domain Name
*/
This. set_cookie = function (name, value, expires, path, domain ){
Var cookieSrt = '';
CookieStr = name + '=' + value + ';';
// Cookie Validity Period
If (expires! = Undefined ){
Expires = parseInt (expires );
Var today = new Date ();
Var time = today. getTime () + expires * 1000;
Var new_date = new Date (time );
Var expiresDate = new_date.toGMTString (); // convert to GMT format.
CookieStr + = 'expires = '+ expiresDate + ';';
}
// Directory
If (path! = Undefined ){
CookieStr + = 'path = '+ path + ';';
}
// Domain Name
If (domain! = Undefined ){
CookieStr + = 'domain = '+ domain + ';';
}
CookieArr. push (cookieStr );
Return true;
}

/*
* Note: set cookies.
* Usage:
* Set Cookie initnode. request. set_cookie ('username', 'initnode', 30 ,'/');
* Name cookie name
* Value cookie value
* Expires validity period, in seconds
* Path valid directory
* Domain Name
*/
This. del_cookie = function (name ){
This. set_cookie (name, '',-999 );
Return true;
}

/*
* Description: cookie settings for header sending
* Usage:
* Set Cookie initnode. request. flush_cookie ();
*/
This. flush_cookie = function (){
Res. setHeader ("Set-Cookie", cookieArr );
}
 
/*
* Description: obtains HTTP header data.
* Usage:
* Obtain a single initnode. request. headers ('Accept ');
* Obtain multiple initnode. request. headers (['Referer', 'Referer']);
* Get all initnode. request. headers ('');
* Parameter: accept referer accept-language user-agent content-type
* Accept-encoding host content-length connection cache-control cookie
*/
This. headers = function (name ){
Return this. _ requestData (name, req. headers );
};

/*
* Description: obtains the Client IP address.
* Usage:
* Initnode. request. getClientIp ();
*/
This. getClientIp = function (){
Var ipAddress;
Var forwardedIpsStr = this. headers ('x-forwarded-');
If (forwardedIpsStr ){
Var forwardedIps = forwardedIpsStr. split (',');
IpAddress = forwardedIps [0];
}
If (! IpAddress ){
IpAddress = req. connection. remoteAddress;
}
Return ipAddress;
};

/*
* Description: obtains the Client IP address.
* Usage:
* Initnode. request. getServer ('hostname ');
* Hostname Host Name
* Type operating system type
* Release hairstyle version
* Uptime Update Time
* Average loadavg Load
* Total totalmem memory
* Freemem idle memory
* Cpus CUP
*/
This. getServer = function (name ){
If (OS [name]) {
Return OS [name] ();
} Else {
Return '';
}
}
 
/*
* Obtain the cookie from the req header and convert it to the object format.
*/
This. _ cookie = function (req ){
Return querystring. parse (req. headers. cookie );
};
 
/*
* Get POSTDATA data
*/
This. _ post = function (_ postData ){
Return querystring. parse (_ postData );
};
 
/*
* GET Data
*/
This. _ get = function (req ){
Var getQuery = url. parse (req. url). query;
Var getData = querystring. parse (getQuery); // getData data
Return getData;
};
 
// Encapsulate the GET and POST operations to obtain data
This. _ requestData = function (name, data ){
If (name = ''){
Return data;
}
If (typeof (name) = 'object') {// passed in as an array
Var temp = {};
For (var I = 0; I <name. length; I ++ ){
If (data [name [I]) {
Temp [name [I] = data [name [I];
} Else {
Temp [name [I] = '';
}
}
Return temp;
} Else {
If (data [name]) {
Return data [name];
} Else {
Return '';
}
}
};
 
}
 
Module. exports = request;
 
Usage:
[Javascript]
Initnode. request. set_cookie ('username', 'zhuli ');
Initnode. request. set_cookie ('age131', 100,300 0 ,'/');
Initnode. request. del_cookie ('age11 ');
Initnode. request. del_cookie ('age12 ');
Initnode. request. del_cookie ('age13 ');
Initnode. request. del_cookie ('age141 ');
Initnode. request. del_cookie ('age131 ');
Initnode. request. flush_cookie ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.