質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

0回答

1093閲覧

Radis+expressをsession利用でエラーに。。

kumakake

総合スコア42

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Redis

Redisは、オープンソースのkey-valueデータストアで、NoSQLに分類されます。すべてのデータをメモリ上に保存するため、処理が極めて高速です。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

0グッド

0クリップ

投稿2021/12/22 17:06

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でも引っ掛けることが出ず、原因がつかめません。
何が問題なのかご教授いただけませんか?

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/12/23 00:00

とりあえず redisClient.on('error', 〜) でエラーを表示してみては。
kumakake

2021/12/23 10:03

ありがとうございます! 下記のように追加してみました。 const redisClient = redis.createClient(); console.dir( redisClient ); redisClient.on( "error", function( err ) { console.log( '****redis error****' ); console.dir( err ); }); redisClient.on( "ready", function() { console.log( '****redis ready****' ); }); 結果として、エラーもreadyも表示しませんでした。 console.dir( redisClient )とやってみたところ、 <ref *1> Commander { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, select: [AsyncFunction: SELECT], subscribe: [Function: SUBSCRIBE], pSubscribe: [Function: PSUBSCRIBE], unsubscribe: [Function: UNSUBSCRIBE], pUnsubscribe: [Function: PUNSUBSCRIBE], quit: [Function: QUIT], json: { self: [Circular *1] }, ft: { self: [Circular *1] }, [Symbol(kCapture)]: false とonがない? 設定が間違えているのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問