|
1 |
| -# Express + Memcached Sessions -> Google App Engine |
| 1 | +## Express.js + Memcached Sessions on Google App Engine |
2 | 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`. |
| 3 | +This is a simple guide to using memcached for session state while running |
| 4 | +[Express.js](http://expressjs.com/) on Google App Engine. Each Google App Engine |
| 5 | +application comes with a memcached service instance, which can be reached with a |
| 6 | +standard memcached driver at `memcache:11211`. This sample uses the |
| 7 | +[connect-memcached](https://github.com/balor/connect-memcached) module to store |
| 8 | +session data in memcached. |
4 | 9 |
|
5 |
| -1. [Create a new Express app](http://expressjs.com/starter/generator.html) |
| 10 | +## Clone the Express.js + Memcached Sessions app |
6 | 11 |
|
7 |
| -2. Create an `app.yaml` in the root of your application with the following contents: |
| 12 | +If you haven't already, copy the repository to your local machine by entering |
| 13 | +the following command in your terminal window: |
8 | 14 |
|
9 |
| - ```yaml |
10 |
| - runtime: nodejs |
11 |
| - vm: true |
12 |
| - env_variables: |
13 |
| - PORT: 8080 |
14 |
| - MEMCACHE_URL: memcache:11211 |
15 |
| - ``` |
| 15 | +``` |
| 16 | +$ git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git |
| 17 | +$ cd nodejs-docs-samples/appengine/express-memcached-session |
| 18 | +``` |
| 19 | + |
| 20 | +Alternatively, you can [download the sample][download] as a zip and extract it. |
| 21 | + |
| 22 | +## Run the app on your local computer |
| 23 | + |
| 24 | +1. Install dependencies. Enter the following command: |
16 | 25 |
|
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 | 26 | ```
|
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 |
| - }); |
| 27 | + $ npm install |
47 | 28 | ```
|
48 | 29 |
|
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` |
| 30 | +2. Run the start script. |
52 | 31 |
|
53 |
| - Run memcached on localhost:11211 by running `memcached` |
54 |
| - |
| 32 | + ```` |
| 33 | + $ npm start |
| 34 | + ``` |
55 | 35 |
|
56 |
| -6. Deploy your app. For convenience, you can use an npm script to run the command. Modify your `package.json` to include: |
| 36 | +3. In your web browser, enter the following address: |
57 | 37 |
|
58 |
| - ```js |
59 |
| - "scripts": { |
60 |
| - "start": "node server.js", |
61 |
| - "deploy": "gcloud preview app deploy app.yaml --set-default --project [project id]" |
62 |
| - } |
63 | 38 | ```
|
| 39 | + $ http://localhost:8080 |
| 40 | + ``` |
| 41 | +
|
| 42 | +You can see the sample app displayed in the page. This page was delivered by the |
| 43 | +Express.js web server running on your computer. |
| 44 | +
|
| 45 | +In your terminal window, press Ctrl+C to exit the web server. |
| 46 | +
|
| 47 | +## Deploy the app to Google Cloud Platform |
| 48 | +
|
| 49 | +In your terminal window, enter the following command to deploy the sample: |
| 50 | +
|
| 51 | +``` |
| 52 | +$ gcloud preview app deploy app.yaml --promote |
| 53 | +``` |
| 54 | +
|
| 55 | +### See the app run in the cloud |
| 56 | +
|
| 57 | +In your web browser, enter the following address: |
| 58 | +
|
| 59 | +``` |
| 60 | +https://<your-project-id>.appspot.com |
| 61 | +``` |
| 62 | +
|
| 63 | +For convenience, you can use an npm script to run the gcloud command. Add these lines to your package.json file: |
| 64 | +
|
| 65 | +``` |
| 66 | +"scripts": { |
| 67 | + "start": "node server.js", |
| 68 | + "deploy": "gcloud preview app deploy app.yaml --promote --project <your-project-id>" |
| 69 | +} |
| 70 | +``` |
| 71 | +
|
| 72 | +At the terminal you can now run the following command to deploy your application: |
| 73 | +
|
| 74 | +``` |
| 75 | +$ npm run deploy |
| 76 | +``` |
| 77 | +
|
| 78 | +## Configuration |
| 79 | +
|
| 80 | +Every Managed VMs application requires an app.yaml file to describe its deployment configuration. |
| 81 | +
|
| 82 | +```yaml |
| 83 | +runtime: nodejs |
| 84 | +vm: true |
| 85 | +env_variables: |
| 86 | + PORT: 8080 |
| 87 | + MEMCACHE_URL: memcache:11211 |
| 88 | +``` |
| 89 | + |
| 90 | +Notice the `MEMCACHE_URL` environment variable–this is where you can reach your |
| 91 | +standard memcached cluster across instances. |
64 | 92 |
|
65 |
| -At the terminal you can now run `npm run deploy` to deploy your application. |
| 93 | +[download]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/archive/master.zip |
0 commit comments