├── .github ├── FUNDING.yml └── dependabot.yml ├── .gitignore ├── Dockerfile ├── README.md ├── docker-compose.yml ├── package.json ├── redis-client.js └── server.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: HugoDF 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 99 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:lts 2 | # Or whatever Node version/image you want 3 | WORKDIR '/var/www/app' 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express Redis Docker app 2 | 3 | Requirements: [Docker Community Edition](https://www.docker.com/community-edition) 4 | 5 | To start the app run: `docker-compose up`. 6 | 7 | It will then be started on port 3000. 8 | 9 | # Endpoints 10 | 11 | ## Hello World 12 | 13 | ```sh 14 | curl http://localhost:3000 15 | ``` 16 | 17 | ## Storing Data 18 | ```sh 19 | curl http://localhost:3000/store/my-key\?some\=value\&some-other\=other-value 20 | ``` 21 | 22 | ## Fetching Data 23 | 24 | ```sh 25 | curl http://localhost:3000/my-key 26 | ``` 27 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | redis: 2 | image: redis 3 | container_name: cache 4 | expose: 5 | - 6379 6 | app: 7 | build: ./ 8 | volumes: 9 | - ./:/var/www/app 10 | links: 11 | - redis 12 | ports: 13 | - 3000:3000 14 | environment: 15 | - REDIS_URL=redis://cache 16 | - NODE_ENV=development 17 | - PORT=3000 18 | command: 19 | sh -c 'npm i && node server.js' 20 | 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-redis", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "server.js", 6 | "scripts": { 7 | "start": "node server.js" 8 | }, 9 | "keywords": [], 10 | "author": "Hugo Di Francesco", 11 | "license": "MIT", 12 | "dependencies": { 13 | "express": "^4.16.3", 14 | "redis": "^3.0.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /redis-client.js: -------------------------------------------------------------------------------- 1 | const redis = require('redis'); 2 | const {promisify} = require('util'); 3 | const client = redis.createClient(process.env.REDIS_URL); 4 | 5 | module.exports = { 6 | ...client, 7 | getAsync: promisify(client.get).bind(client), 8 | setAsync: promisify(client.set).bind(client), 9 | keysAsync: promisify(client.keys).bind(client) 10 | }; 11 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | 4 | 5 | const redisClient = require('./redis-client'); 6 | 7 | app.get('/store/:key', async (req, res) => { 8 | const { key } = req.params; 9 | const value = req.query; 10 | await redisClient.setAsync(key, JSON.stringify(value)); 11 | return res.send('Success'); 12 | }); 13 | 14 | app.get('/:key', async (req, res) => { 15 | const { key } = req.params; 16 | const rawData = await redisClient.getAsync(key); 17 | return res.json(JSON.parse(rawData)); 18 | }); 19 | 20 | app.get('/', (req, res) => { 21 | return res.send('Hello world'); 22 | }); 23 | 24 | const PORT = process.env.PORT || 3000; 25 | app.listen(PORT, () => { 26 | console.log(`Server listening on port ${PORT}`); 27 | }); 28 | --------------------------------------------------------------------------------