Created
September 5, 2021 15:37
-
-
Save thoughtspeed7/c862d66b8e632234ae2189ed135acf13 to your computer and use it in GitHub Desktop.
Connect To Redis Memorystore From Cloud Functions, Cloud Run and App Engine Using Serverless VPC Access | Google Cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// om namah shivaya | |
// require scripts | |
const { promisify } = require('util'); | |
const redis = require('redis'); | |
const express = require('express'); | |
startService(); | |
async function startService() { | |
// replace 127.0.0.1 with your redis instance ip | |
const redisHost = '127.0.0.1'; | |
const client = redis.createClient({ | |
host: redisHost, | |
}); | |
const incrAsync = promisify(client.incr).bind(client); | |
const app = express(); | |
const port = process.env.PORT || 4000; | |
app.get('/', async (req, res) => { | |
res.send(`counter: ${await incrAsync('counter')}`); | |
}); | |
app.listen(port, () => { | |
console.log(`app-engine listening on port ${port}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment