Monday, 15 May 2017

How to send a response to a POST request?

I'am using the 'request' library to send HTTP POST request from server.js to authentication.js. Then I want to send back a response from authentication.js to server.js, how can I do that?

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
          });
          res.token = token;
          return res.token;
          console.log(token);
        }
        //I NEED TO SEND BACK THE TOKEN FROM HERE
      }
    })
  });



via Mit

No comments:

Post a Comment