1

I'm quite new to using express. I'm using express, react, and mongodb to create a full stack app. When storing the sessionID on the Express server, I'm using a redis store with the "redis" and "connect-redis" packages.

For the most part this structure works, however, there is an error that occurs when I try to log out that messes up the server.

I can log in (create profiles, get posts etc) without any errors but for some reason once I try to log out, I get this TypeError: client.get is not a function Error. Even if I try restarting my two servers, this error message keeps on popping up in my terminal. Furthermore, once I start getting these TypeError functions, even if I restart my servers, whenever I log in (/ do any requests of an API endpoint) I now get the error message "TypeError: client.set is not a function". I can log in fine, but this message pops up. Then when I try to log out I get the client.get error and the client.set error as well as a response status of 500.

I'd be extremely grateful if anyone has any insight in solving this error. And perhaps, Redis is not the best fast-querying database that I should use to store sessions.

Some additional details:

  • I installed redis, connect redis using the command "npm i redis" and "npm i connect-redis"
  • I will post my router orders, the error I get in the terminal, my logout function, index.js, redis set up, and session set up
  • The error when logging out does not get caught in my err if statement in the logout function. The error gets caught in my global error catching function

Session Implementation

/myapp/middleware/session.js

import session from "express-session";
import connectRedis from "connect-redis";
import RedisStore from "connect-redis";

// Database
import redisClient from "../db/redis.js"
 
// Initialize store. 
let redisStore = new RedisStore({ 
    client: redisClient,
    prefix: "myapp:",
  })

// Connecting Session with Redis server
export default session(
    {
        store: redisStore,
        secret: 'SOME_PASSWORD', 
        resave: false, 
        saveUninitialized: false, the session,
        name: "sessionID",
        cookie: {      
            secure: false, 
            httpOnly: true, 
            maxAge: 1000 * 60 * 30 , 
        } 
    }
);


/myapp/db/redis.js

Redis Implementation


import {createClient} from 'redis'; 
import session from "express-session";

// Error
import ApiError from "../error/ApiError.js"


// Configuration of Redis Instance

// Initialize client.
  let redisClient = createClient(); 

  try{
    console.log("Creating client")
    redisClient.connect()
  } catch(err){
    console.error("Redis error: ", err)
    throw ApiError.redisError("Error with Redis")
  }
  
export default { redisClient }
  

/myapp/index.js

Index.js Implementation


import express from 'express';
import cors from "cors";
import authServer from "./routes/authServer.js";
import session from "./middleware/session.js";
import redisClient from "./db/redis.js"


appAuthServer.use(express.json());
appAuthServer.use(session);

appAuthServer.use(cors())

appAuthServer.use('/users', authServer)

appAuthServer.get('*', (req, res) => {
  res.send("Route not found", 404)
})

appAuthServer.listen(AUTH_SERVER_PORT, ()=>{
  console.log(`Auth Server is running on port: ${AUTH_SERVER_PORT}`)
})

appAuthServer.use(apiErrorHandler)



export default { 
  appAuthServer
};

Sub router implementation /myapp/routers/posts.js

const router = express.Router(); 

// Allows express to use json 
router.use(express.json());


router.post('/createProfile', authController.createProfile);

router.post('/login', authenticateLogin, authController.login)

router.use(authenticateSession)

router.post('/logout', authController.logout)

router.delete("/deleteAccount", authController.deleteAccount);

Log out function implementation

function logout(req, res, next){
    console.log("In the log out function")
    req.session.destroy(req.sessionID, (err) => {
        if (err) { 
            next(ApiError.internal("Error in logging out"))
        }
        console.log("Destroyed session")
    })
    return res.redirect('/');
}


Terminal errors

Stackerror

StackError Number#2

Things I've tried:

All of these things I've tried didn't change the behavior of my program.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.