A universal cookie library.
npm i cookie-machine -S
Although tests are running on node >=4.0
(because of jsdom), the library should support node >=0.12
.
Currently, only CommonJS is supported. Please submit an issue if you'd like any other module to be supported. Although, browser builds have low chances of being supported because it doesn't make any sense.
// server-side / node
var cookie = require('cookie-machine');
http.createServer(function(req, res) {
cookie.init(res);
cookie.set('yolo', 'swag');
});
// browser
var cookie = require('cookie-machine');
// notice that we no longer have to call `init` here.
// init is only required for the server-side.
cookie.set('yolo', 'swag');
Check out more examples.
Here are some of the methods exposed by the library:
res
- An object which is expected to be node's nativeresponse
.
Initializes cookie
to be ready for node. In the client, this is a noop
.
key
- The key of the cookie to be get. Returns the value of the key in the cookie.
key
- The key of the cookie to be stored.value
- The value of the cookie to be stored.options
- (optional
, defaults to{ expires: new Date(0) }
) Cookie options such asexpired
,domain
,path
, and etc.
Set a value in the cookie.
key
- Key in the cookie to be removed.options
- (optional
, defaults to{ expires: new Date(0) }
) Cookie options such asexpired
,domain
,path
, and etc.
Remove a key in the cookie.
This project was motivated by cookie-dough.