我是Node.js的初学者,我正试图想出一种方法来在另一个api中调用api的输出。例如,我有一个像这样的api:
app.post('/login', function (req, res)
{
email = req.headers.email;
password = req.headers.password
var status = '';
var type = '';
if(req.headers.email && req.headers.password)
{
connection.query('SELECT username FROM user WHERE email = "'+req.headers.email+'"and password = "'+req.headers.password+'"', function (error, rows, fields) {
if(rows.length == 1)
{
type = 'local';
console.log(req.headers.email);
connection.query('UPDATE user SET last_login = now() WHERE (email) = "'+req.headers.email+'"'); //update time of login
//var token = jwt.encode({Type: type, Username: rows[0].username, Last_login: rows[0].last_login},secret,'HS512');
var token = jwt2.sign({type:type,username:rows[0].username,last_login:rows[0].last_login},secret,'HS512',{expiresInMinutes:1});
console.log('token is :'+JSON.stringify(token));
// var decoded = jwt.decode(token, secret);
console.log('sucessful login');
//res.end('"status:"\n'+JSON.stringify(token)); //output as JSON
//token = JSON.stringify(token);
status = 'success';
//res.writeHead(200,{'Content-length':token.length,'Access-Token':token});
res.writeHead(200,{'Content-length':token.length,'Content-Type':'application/json','Access-Token':token});
res.end(status);
console.log(status);
}
if(rows.length == 0)
{
connection.query('select * from temp_users where email = ? and password = ?',[email,password],function(error,rows,fields)
{
if(rows.length == 1)
{
status = 'pending';
//res.end('Your Account needs to be verified');
res.end('"status:"'+JSON.stringify(status));
console.log('Your Account needs to be verified.');
}
else
{
status = 'Failed';
res.end('"status:"'+JSON.stringify(status));
//res.end('no such user.Please create an account');
console.log('no such user.Please create an account');
}
}
);
}
});
}
else {
console.log('no headers');
}
});
我有另一个这样的api:
app.post('/feed',function(req,res)
{
//console.log(req.headers['useracesstoken']);
// access_token = req.headers['useracesstoken']; //Access token from header
console.log(req.headers);
};
现在我想使用/login
api的输出,以便我可以在/feed
api中解码令牌变量。我怎么能这样做?