简体   繁体   English

获取/发布请求 node.js

[英]Get/Post request node.js

I have input for username and password, when user click submit, they are sent with POST method, and when node.js receives them, they will be put in constructor class, something like this:我已经输入了用户名和密码,当用户点击提交时,它们是通过 POST 方法发送的,当 node.js 收到它们时,它们将被放入构造函数 class 中,如下所示:

app.post('submit1', (req, res) => {
    var data = req.body;
    new User(data);
    res.send(i want to send response if that bellow is true)
})

After that, in class User, there is a function loginClient(data), and inside that function is function for verification waiting for some emit, and it that functions receives that emit, username and password are correct, and I want to replay to that 'submit1'.. How can I do this? After that, in class User, there is a function loginClient(data), and inside that function is function for verification waiting for some emit, and it that functions receives that emit, username and password are correct, and I want to replay to that 'submit1'.. 我该怎么做?

class User { 
  loginClient(data){ 
    this.somthing.on('thatEmit', () => { 
      "send response because this function got that emit" 
    }); 
  } 
}

In your case, you'd have to add a callback inside the function located inside the user constructor.在您的情况下,您必须在位于用户构造函数内的 function 中添加一个回调。

app.post('submit1', (req, res) => {
    var data = req.body;
    var user = new User(data);
    user.loginClient(data, function(valid){
    if(valid){
      res.send(i want to send response if that bellow is true)
    }else{
     res.send('false')
   }
    })   
})

Then inside your User class your function for instance would be loginClient然后在您的用户 class 中,您的 function 例如将是 loginClient

function loginClient(callback){
//do whatever you need to do to validate
  this.somthing.on('thatEmit', () => { 
      //"send response because this function got that emit" 
     if(callback){
      callback(true) //this will call function that's in the post
      }
   }); 
 
}

A response from the server is compulsory, no matter it is correct or not.服务器的响应是强制性的,无论它是否正确。 Your verification function that receives arguments can return a value like this:您的验证 function 接收 arguments 可以返回如下值:

var x = funct(argument)

function funct(argument) {
// do something
return something
}

However, you can send messages like true or false in your response.但是,您可以在回复中发送truefalse等消息。

Like:喜欢:

res.send('true')

or或者

res.send('false')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM