Rediをセッションストアに利用しようとしてサンプルを参照に下記のようなソースを作成しました。
nodejs
1const express = require('express') 2require('express-async-errors'); 3 4const cookieParser = require('cookie-parser') 5const session = require('express-session') 6const redis = require('redis') 7 8const app = express() 9 10const RedisStore = require('connect-redis')(session) 11const redisClient = redis.createClient() 12 13app.use(cookieParser()) 14app.use(session({ 15 secret: 'secret_key', 16 resave: false, 17 saveUninitialized: false, 18 store: new RedisStore({ client: redisClient }), 19 cookie: { httpOnly: true, secure: false, maxage: 1000 * 60 * 30 } 20})) 21 22app.get('/', (req, res) => { 23 if (req.session.views) { 24 req.session.views++ 25 } else { 26 req.session.views = 1 27 } 28 res.json({ 29 'sessionID': req.sessionID, 30 'req.session.views': req.session.views 31 }) 32}) 33 34app.listen(3001, () => console.log('Example app listening on port 3001!'))
docker-compose.ymlの内容
% cat docker-compose.yml
version: '3'
services:
redis:
image: "redis:latest"
ports:
- "6379:6379"
volumes:
- "./data/reis:/data"
1)dockerを起動します。
% docker-compose up -d
2)アプリを起動します。
% node app.js
と実行すると、下記のエラーが表示されます。
% node app.js
Example app listening on port 3001!
(node:34971) UnhandledPromiseRejectionWarning: Error: The client is closed
at Commander._RedisClient_sendCommand (/Users/hoge/redistest/node_modules/@node-redis/client/dist/lib/client/index.js:393:31)
at Commander.commandsExecutor (/Users/hoge/redistest/node_modules/@node-redis/client/dist/lib/client/index.js:160:154)
at Commander.BaseClass.<computed> [as get] (/Users/hoge/redistest/node_modules/@node-redis/client/dist/lib/commander.js:8:29)
at RedisStore.get (/Users/hoge/redistest/node_modules/connect-redis/lib/connect-redis.js:33:19)
at session (/Users/hoge/redistest/node_modules/express-session/index.js:485:11)
at newFn (/Users/hoge/redistest/node_modules/express-async-errors/index.js:16:20)
at Layer.handle [as handle_request] (/Users/hoge/redistest/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/hoge/redistest/node_modules/express/lib/router/index.js:323:13)
at /Users/hoge/redistest/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/hoge/redistest/node_modules/express/lib/router/index.js:341:12)
(Use node --trace-warnings ...
to show where the warning was created)
(node:34971) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:34971) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
try cacheでも引っ掛けることが出ず、原因がつかめません。
何が問題なのかご教授いただけませんか?
よろしくお願いします。
あなたの回答
tips
プレビュー