Q:

Setting Session for functions in Node Js

I have a web application I'm working on using Node JS and am running into a problem with the session store in req.
I currently have the sessions working by having my app.js use:
// Enable sessions
app.use(session({
  secret: '***********',
    resave: 'true',
  saveUninitialized: true
}));

app.use(passport.initialize());
app.use(passport.session());

// view engine setup
app.engine('.hbs', exphbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', '.hbs');

var routes = require('./routes/index')(passport, mongoose);

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(__dirname + '/node_modules'));

app.use('/', routes);

and my routes.js:
var express = require('express');
var https = require('https');
var passport = require('passport');
var router = express.Router();
var User = require('../models/User.js');
var Api = require('./api.js');
var request = require('request');
var URL = "https://api.************.com";

module.exports = function(passport, mongoose) {

//main
router.get('/', function(req, res) {
    console.log("Session: " + req.session.test);
  res.render('partials/index', { layout: 'main' });
});

router.get('/login', 
  passport.authenticate('**********', { failureRedirect: '/login' }),
  function(req, res) {
    req.session.test = "test";
    // Successful authentication

var Api = require('./api.js');

Api.getUser(); < ERROR Happens in this file

});

router.get('/home/logout', function(req, res) {

    req.logout();
    req.session.destroy();
    res.redirect('/');

});

    return router;

}

Now I'm trying to create a separate file called API.js to store API calls to a third party api with functions like:
var express = require('express');
var request = require('request');
var https = require('https');
var passport = require('passport');
var User = require('../models/User.js');
var Api = require('./api.js');
var URL = "https://api.******.com";

exports.getUser = function(req, result) {

    console.log(req.session.test); <ERROR IS HERE

    request.get({
        uri: URL + '/user/me/',
        headers: {Authorization: 'bearer ' + req.user.accessToken},
        json: true
      }, function(e, r, body) {

            if(e) {

                return "{error: true}";

            }

        return body;

    });

};

exports.getVehicles = function(req, result) {

    request.get({
        uri: URL + '/vehicles/',
        headers: {Authorization: 'bearer ' + req.user.accessToken},
        json: true
      }, function(e, r, body) {

            if(e) {

                return "{error: true}";

            }

        return body;

    });

};

However everytime I call the functions from routes.js I get the following error: 
TypeError: Cannot read property 'session' of undefined
I also get the error on req.user when I try and call it there.
From the routes I'm just calling:
var Api = require('./api.js');
Api.getUser();
Any ideas on how to pass the session to the new file or any functions?

A:

You don't need to recall and re-instantiate the API since you already define it once.
you need to pass in the req object simply. 

In your route.js
var express = require('express');
var https = require('https');
var passport = require('passport');
var router = express.Router();
var User = require('../models/User.js');
var Api = require('./api.js');
var request = require('request');
var URL = "https://api.************.com";

module.exports = function(passport, mongoose) {

//main
router.get('/', function(req, res) {
    console.log("Session: " + req.session.test);
  res.render('partials/index', { layout: 'main' });
});

router.get('/login', 
  passport.authenticate('**********', { failureRedirect: '/login' }),
  function(req, res) {
    req.session.test = "test";
    //// Successful authentication

    Api.getUser(res); 
});

router.get('/home/logout', function(req, res) {

    req.logout();
    req.session.destroy();
    res.redirect('/');

});

    return router;

}

