Skip to content

Commit adc1964

Browse files
committed
Merge pull request #14 from JustinBeckwith/master
Add sample for using memcached for connect session state
2 parents 2af8d34 + 24387c3 commit adc1964

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Express + Memcached Sessions -> Google App Engine
2+
3+
This is a simple guide to using memcached for session state while running [expressjs](http://expressjs.com/) on Google App Engine. Each Google App Engine application comes with a memcached service instance, which can be reached with a standard memcached driver at `memcache:11211`.
4+
5+
1. [Create a new Express app](http://expressjs.com/starter/generator.html)
6+
7+
2. Create an `app.yaml` in the root of your application with the following contents:
8+
9+
```yaml
10+
runtime: nodejs
11+
vm: true
12+
env_variables:
13+
PORT: 8080
14+
MEMCACHE_URL: memcache:11211
15+
```
16+
17+
Notice the MEMCACHE_URL environment variable - this is where you can reach your standard memcached cluster across instances.
18+
19+
3. Use the [connect-memcached](https://github.com/balor/connect-memcached) module. Run `npm install --save connect-memcached`, and add the following to your server.js or app.js:
20+
21+
```js
22+
var MemcachedStore = require('connect-memcached')(session);
23+
...
24+
app.use(session({
25+
secret: 'appengineFTW',
26+
key: 'test',
27+
proxy: 'true',
28+
store: new MemcachedStore({
29+
hosts: [process.env.MEMCACHE_URL || '127.0.0.1:11211']
30+
})
31+
}));
32+
```
33+
4. In your express route handlers, you can now safely use `req.session.*` across multiple nodejs instances:
34+
35+
```js
36+
app.get('/', function(req, res){
37+
publicIp.v4(function (err, ip) {
38+
res.write("<div>" + ip + "</div>");
39+
if(req.session.views) {
40+
++req.session.views;
41+
} else {
42+
req.session.views = 1;
43+
}
44+
res.end('Viewed <strong>' + req.session.views + '</strong> times.');
45+
});
46+
});
47+
```
48+
49+
5. To test the sample locally, you can install memcached.
50+
- OSX + [Brew](http://brew.sh/): `brew install memcached`
51+
- Windows + [Chocolatey](https://chocolatey.org/packages/memcached): `choco install memcached`
52+
53+
Run memcached on localhost:11211 by running `memcached`
54+
55+
56+
6. Deploy your app. For convenience, you can use an npm script to run the command. Modify your `package.json` to include:
57+
58+
```js
59+
"scripts": {
60+
"start": "node server.js",
61+
"deploy": "gcloud preview app deploy app.yaml --set-default --project [project id]"
62+
}
63+
```
64+
65+
At the terminal you can now run `npm run deploy` to deploy your application.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
runtime: nodejs
15+
api_version: 1
16+
vm: true
17+
env_variables:
18+
PORT: 8080
19+
MEMCACHE_URL: localhost:11211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "express-memcached-session-demo",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "gcloud preview app deploy app.yaml --set-default --project express-memcached-demo"
7+
},
8+
"dependencies": {
9+
"connect-memcached": "^0.1.0",
10+
"cookie-parser": "~1.3.5",
11+
"express": "~4.12.4",
12+
"express-session": "^1.11.3",
13+
"public-ip": "^1.1.0"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var session = require('express-session');
18+
var cookieParser = require('cookie-parser');
19+
var http = require('http');
20+
var MemcachedStore = require('connect-memcached')(session);
21+
var publicIp = require('public-ip');
22+
23+
var app = express();
24+
25+
app.use(cookieParser());
26+
app.use(session({
27+
secret: 'appengineFTW',
28+
key: 'test',
29+
proxy: 'true',
30+
store: new MemcachedStore({
31+
hosts: [process.env.MEMCACHE_URL || '127.0.0.1:11211']
32+
})
33+
}));
34+
35+
app.get('/', function(req, res){
36+
publicIp.v4(function (err, ip) {
37+
38+
// This shows the IP for each
39+
res.write('<div>' + ip + '</div>');
40+
41+
if(req.session.views) {
42+
++req.session.views;
43+
} else {
44+
req.session.views = 1;
45+
}
46+
res.end('Viewed <strong>' + req.session.views + '</strong> times.');
47+
});
48+
});
49+
50+
http.createServer(app).listen(process.env.PORT || 8080, function() {
51+
console.log('Listening on %d', this.address().port);
52+
});

0 commit comments

Comments
 (0)