我正在使用'request'库从server.js向authentication.js发送HTTP POST请求。然后我想将authentication.js的响应发送回server.js,我该怎么做?
server.js
socket.on('client:authentication', function(authObject){
request.post(
'http://localhost:8090/authenticate',
{ json: { username: authObject.username, password: authObject.password } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
});
authentication.js
app.post('/authenticate', function(req, res){
User.findOne({
username: req.body.username
}, function(err, user){
if(!user){
console.log('Authentication failed. User not found');
}
else if(user){
if(user.password != req.body.password){
console.log('Authentication failed. Wrong password');
}
else{
var token = jwt.sign(user, app.get('secretWord'), {
expiresIn : 10800
});
//I NEED TO SEND BACK THE TOKEN FROM HERE
}
}
})
});
答案 0 :(得分:2)
试试这个:
res.send({
token: token
});