Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample for using memcached for connect session state #14

Merged
merged 2 commits into from
Nov 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions appengine/express-memcached-session/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Express + Memcached Sessions -> Google App Engine

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`.

1. [Create a new Express app](http://expressjs.com/starter/generator.html)

2. Create an `app.yaml` in the root of your application with the following contents:

```yaml
runtime: nodejs
vm: true
env_variables:
PORT: 8080
MEMCACHE_URL: memcache:11211
```

Notice the MEMCACHE_URL environment variable - this is where you can reach your standard memcached cluster across instances.

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:

```js
var MemcachedStore = require('connect-memcached')(session);
...
app.use(session({
secret: 'appengineFTW',
key: 'test',
proxy: 'true',
store: new MemcachedStore({
hosts: [process.env.MEMCACHE_URL || '127.0.0.1:11211']
})
}));
```
4. In your express route handlers, you can now safely use `req.session.*` across multiple nodejs instances:

```js
app.get('/', function(req, res){
publicIp.v4(function (err, ip) {
res.write("<div>" + ip + "</div>");
if(req.session.views) {
++req.session.views;
} else {
req.session.views = 1;
}
res.end('Viewed <strong>' + req.session.views + '</strong> times.');
});
});
```

5. To test the sample locally, you can install memcached.
- OSX + [Brew](http://brew.sh/): `brew install memcached`
- Windows + [Chocolatey](https://chocolatey.org/packages/memcached): `choco install memcached`

Run memcached on localhost:11211 by running `memcached`


6. Deploy your app. For convenience, you can use an npm script to run the command. Modify your `package.json` to include:

```js
"scripts": {
"start": "node server.js",
"deploy": "gcloud preview app deploy app.yaml --set-default --project [project id]"
}
```

At the terminal you can now run `npm run deploy` to deploy your application.
19 changes: 19 additions & 0 deletions appengine/express-memcached-session/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2015, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: nodejs
api_version: 1
vm: true
env_variables:
PORT: 8080
MEMCACHE_URL: localhost:11211
15 changes: 15 additions & 0 deletions appengine/express-memcached-session/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"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",
"cookie-parser": "~1.3.5",
"express": "~4.12.4",
"express-session": "^1.11.3",
"public-ip": "^1.1.0"
}
}
52 changes: 52 additions & 0 deletions appengine/express-memcached-session/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2015, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

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 publicIp = require('public-ip');

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){
publicIp.v4(function (err, ip) {

// This shows the IP for each
res.write('<div>' + ip + '</div>');

if(req.session.views) {
++req.session.views;
} else {
req.session.views = 1;
}
res.end('Viewed <strong>' + req.session.views + '</strong> times.');
});
});

http.createServer(app).listen(process.env.PORT || 8080, function() {
console.log('Listening on %d', this.address().port);
});