Skip to content

Instantly share code, notes, and snippets.

@JustinBeckwith
Created October 6, 2015 04:41
Show Gist options
  • Save JustinBeckwith/e25983cd50ab21a6b8ad to your computer and use it in GitHub Desktop.
Save JustinBeckwith/e25983cd50ab21a6b8ad to your computer and use it in GitHub Desktop.
Using memcached for session state with express and nodejs on AppEngine
runtime: nodejs
api_version: 1
vm: true
env_variables:
PORT: 8080
MEMCACHE_URL: memcache:11211
{
"name": "express-memcached-session-demo",
"version": "1.0.0",
"private": true,
"scripts": {
"deploy": "gcloud preview app deploy app.yaml --set-default --project express-memcached-demo"
},
"dependencies": {
"connect-memcached": "^0.1.0",
"express": "~4.12.4",
"express-session": "^1.11.3",
"cookie-parser": "~1.3.5"
}
}
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var http = require('http');
var MemcachedStore = require('connect-memcached')(session);
var app = express();
app.use(cookieParser());
app.use(session({
secret: 'appengineFTW',
key: 'test',
proxy: 'true',
store: new MemcachedStore({
hosts: [process.env.MEMCACHE_URL || '127.0.0.1:11211']
})
}));
app.get('/', function(req, res){
if(req.session.views) {
++req.session.views;
} else {
req.session.views = 1;
}
res.send('Viewed <strong>' + req.session.views + '</strong> times.');
});
http.createServer(app).listen(process.env.PORT || 8080, function() {
console.log("Listening on %d", this.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment