我有一个Web应用程序,我正在使用Node JS,并且在req中遇到了会话存储的问题。
我目前通过让我的app.js使用来完成会话:
// 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);
和我的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;
}
现在我正在尝试创建一个名为API.js的单独文件,以便将API调用存储到具有以下功能的第三方API:
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;
});
};
但是每当我调用routes.js中的函数时,我都会收到以下错误:
TypeError:无法读取未定义
的属性'session'当我尝试在那里调用时,我也在req.user上收到错误。
从我刚刚打电话的路线:
var Api = require('./ api.js');
Api.getUser();
有关如何将会话传递给新文件或任何功能的任何想法?
答案 0 :(得分:1)
在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;
}