Skip to content

Commit

Permalink
Initial import for tutorial for handling sessions (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
  • Loading branch information
fhinkel authored Aug 5, 2019
1 parent 7e2485b commit 90a4cdb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sessions/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2019, Google LLC.
# 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.
#
[START getting_started_sessions_runtime]
runtime: nodejs10
[END getting_started_sessions_runtime]

49 changes: 49 additions & 0 deletions sessions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019, Google LLC.
// 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.

const {Firestore} = require('@google-cloud/firestore');
const express = require('express');
const session = require('express-session');

const app = express();
const {FirestoreStore} = require('@google-cloud/connect-firestore');

app.use(
session({
store: new FirestoreStore({
dataset: new Firestore({
kind: 'express-sessions',
}),
}),
secret: 'my-secret',
resave: false,
saveUninitialized: true,
})
);

const colors = ['red', 'blue', 'green', 'yellow', 'pink'];

app.get('/', (req, res) => {
console.log(req.session.id);
if (!req.session.views) {
req.session.views = 0;
req.session.color = colors[Math.floor(Math.random() * 5)];
}
const views = req.session.views++;
res.send(`<body bgcolor=${req.session.color}>Views ${views}</body>`);
});

const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});
17 changes: 17 additions & 0 deletions sessions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "session-handling",
"private": true,
"description": "Session Handling via Firestore Tutorial",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@google-cloud/connect-firestore": "^1.0.3",
"@google-cloud/firestore": "^1.3.0",
"express": "^4.17.1",
"express-session": "^1.16.2"
}
}

0 comments on commit 90a4cdb

Please sign in to comment.