javascript – i can’t connect redis with node js to save sessions
this is my code iam using express
iam trying to connect and save sessions in redis database
the redis is runnig i checked it
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const session = require('express-session');
const Redis = require('redis');
const RedisStore = require('connect-redis')(session);
const redisClient = Redis.createClient({
host: '127.0.0.1',
port: 6379,
});
require('dotenv').config();
const indexRouter = require('./routes/index');
const authRouter = require('./routes/auth');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const sessionConfig = {
store: new RedisStore({ client: redisClient }),
resave: false,
saveUninitialized: true,
secret: process.env.SECRET,
cookie: {
secure: false,
httpOnly: false,
maxAge: 1000 * 60 * 10,
},
};
app.use(session(sessionConfig));
app.use("https://stackoverflow.com/", indexRouter);
app.use('/auth', authRouter);
module.exports = app;
{
"name": "redis",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon ./bin/www"
},
"dependencies": {
"connect-redis": "^7.1.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dotenv": "^16.3.1",
"express": "~4.16.1",
"express-session": "^1.17.3",
"ioredis": "^5.3.2",
"morgan": "~1.9.1",
"redis": "^4.6.10"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
i deleted node modules and package json lock, changed node v and restarted the machine but
iam always getting this error
C:\Users\ahmed\OneDrive\Desktop\redis\app.js:7
const RedisStore = require('connect-redis')(session);
^
TypeError: require(...) is not a function
at Object.<anonymous> (C:\Users\ahmed\OneDrive\Desktop\redis\app.js:7:44)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Function.Module._load (node:internal/modules/cjs/loader:828:14)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object.<anonymous> (C:\Users\ahmed\OneDrive\Desktop\redis\bin\www:7:11)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
[nodemon] app crashed - waiting for file changes before starting...
i deleted node modules and package json lock, changed node v and restarted the machine and checked that the database is running
iam always getting this error
Read more here: Source link
