![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@idagio/cookie-middleware
Advanced tools
Middleware for dealing with the parsing and serialization of cookies for Node.js / Express.js
Middleware for dealing with the parsing and serialization of cookies for Node.js / Express.js
This middleware is very simplistic: It does exactly what it says on the tin – it gives you a way to work with cookies. What this middleware does not do:
For the above things, you should write a module that does what you need, handling asynchronicity as it arises: don't try to make something asynchronous look synchronous.
At IDAGIO, we use two modules that deal with session management & authentication. Only one is open-source, because the other contains way too much application specific code to be made generic.
Our session management module is available as: @IDAGIO/session-middleware
The very basic usage of this module is to use the exported constructor on plain request
and response
objects. Below is an example of such:
var http = require('http');
var Cookies = require('@idagio/cookie-middleware');
var internals = {};
internals.handleRequest = function(request, response) {
request.cookies = new Cookies(request, response);
// Do something with request.cookies
//
// For instance, set a cookie: `request.cookies.set('foo', 'bar')`
//
// Or get a cookie: request.cookies.get('foo')
response.writeHead(200);
response.end('Done!');
};
http.createServer(internals.handleRequest).listen(8080);
Alternatively, you can use the middleware that is exported as well, for instance:
var express = require('express');
var Cookies = require('@idagio/cookie-middleware');
var app = express();
app.use(Cookies.middleware);
app.get('/', function(request, response) {
// Use request.cookies just as above
});
app.listen(3000);
var express = require('express');
var Cookies = require('@idagio/cookie-middleware');
var Redis = Redis.createClient(process.env.REDIS_URL);
var app = express();
app.use(Cookies.middleware);
app.use(function(request, response, next) {
var sessionId = request.cookies.get('my_app_session');
if (!sessionId) {
return next();
}
redis.get('session:'+sessionId, function(err, data) {
if (!err && data) {
request.session = data;
}
next();
});
});
app.get('/', function(request, response) {
response.writeHead(200);
if (request.session) {
response.end('You have a session!');
} else {
response.end('You do not have a session :(')
}
});
app.listen(3000);
new Cookies(request, response)
Creates an instance of the cookie handler using Node.js request
and response
objects.
This will override response.end
via the js-http onHeaders module.
Cookie parsing is delegated to the js-http cookie module, however, this only happens when you use the Cookies#get
method to retrieve a cookie's value.
Cookies.prototype.get(name)
Parses the cookies and returns the cookie with the given name as a String.
Cookies.prototype.set(name, value, [ options = {}, [ override=false ] ])
Sets a cookie to be sent as part of the Set-Cookie header. The options
object is passed directly into the cookie
modules' serialize function. For the various available options, see that module's documentation. The override
parameter allows you to clear any other value that may have been set for a cookie with the given name.
Calling set
multiple times with the same name will result in multiple cookies all with the same name.
Cookies.prototype.unset(name)
Shorthand for Cookies.prototype.set(name, '', { expires: new Date(0) }, true);
.
Cookies.middleware(request, response, next)
Standard connect / express.js middleware. Creates an instance of Cookies
stored as request.cookies
.
FAQs
Middleware for dealing with the parsing and serialization of cookies for Node.js / Express.js
We found that @idagio/cookie-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.