1 year ago

#77458

test-img

itsninjabunny

Is there a way to send a session to routes.js?

so I have a separate js file that does all of my routing. Is it possible to send a session to that routing? or do I have to do it all through my app.js?

app.js

const express = require('express');
const routes = require('./routes/routes.js');
const session = require('express-session'); //handles all of my routing
const PORT = 3000;
const app = express();

app.use(session({
    secret: 'this is my secret',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true }
}));

app.post('/login', routes.login); 

routes.js

var session;

exports.login = (req, res) => {
     session = req.session.user = {
          user_id : req.body.user_id,
          isAuthenticated : true,
     };
}

or do I have to do it like this?

var session;

app.post('/login', (req, res) => {
      session = req.session.user = {
           user_id : req.body.user_id,
           isAuthenticated : true,
      };
})

I simply write my code like this because I only want my routes in my app.js because I think it's better to only have routes there else someone gives a valid reason to keep everything in my app.js. This is the only way I know how to route my endpoints. Any help or advice is much appreciated!

javascript

node.js

express

0 Answers

Your Answer

Accepted video resources