Skip to content

Instantly share code, notes, and snippets.

@soarez soarez/server.js
Created Oct 20, 2014

Embed
What would you like to do?
REST my Cookies
/*
* npm i express cors cookie-parser && node server
*/
var url = require('url');
var util = require('util');
var assert = require('assert');
var express = require('express');
var cors = require('cors');
var cookieParser = require('cookie-parser');
var app = express();
var port = 9999;
app.use(cookieParser());
app.use(cors({
credentials: true,
origin: function(origin, cb){ cb(null, true); }
}));
app.get('/', function(req, res) {
console.log('listing', req.cookies);
res.send(req.cookies);
});
app.get('/:key', function(req, res) {
var key = req.params.key;
console.log('get', key, ':: cookies:', req.cookies);
res
.status(200)
.type('text/plain')
.send(req.cookies[key])
.end();
});
app.put('/:key/:value?', function(req, res) {
var key = req.params.key;
var value = req.params.value;
console.log('set', key, 'to', value, ':: cookies:', req.cookies);
res
.cookie(key, value)
.status(200)
.type('text/plain')
.send('set ' + key + ' to ' + value)
.end();
});
app.delete('/:key', function(req, res) {
var key = req.params.key;
console.log('clearing', key);
res
.clearCookie(key)
.status(200)
.type('text/plain')
.send('cleared ' + key)
.end();
});
app.listen(port, function() { console.log('Running'); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.