I'm using Node JS with Express. I have a callback Post which takes in the username and password from the user, generates a token from within another callback. My problem is accessing this token globally (across the application)
app.post('/custom', function (req, res, next) {
//Get Username, password using body parser
authService1.getToken({
url: config.url
}, function (err, token) {
if (err) {
next(err); //handling error
} else {
setToken(token);
res.send(token);
//token is accessible here
}
});
});
function setToken(token) {
global.t = token;
}
I have seen this question. I know AJAX is asynchronous. I'm trying to access the variable t in other JS files. There is no "use strict" pragma used on any of the JS files, so usage or declaration of a global variable isn't a problem (I guess). I'm OK with the design/maintenance issues that arise by using a global variable, so exports/locals is not a solution for me. Also, I want to be able to access this variable from outside callback functions (without using req/sessions variables)
What am I missing here? (I can access the token from within the else part)