0

I'm a newer to node.I wirte this test case to r/w mysql databases. However, after click the register button. The table in the database is updated, while the client has no response. At start,it shows the socket hang up error. After i update my node to v.0.8.25. It show nothing now. Anyone can help me with this?

my client code is very simple:

var username = $('#username').val().trim();
var pwd = $('#password').val().trim();
$.post('http://myserver/' + 'reg', {username : username , password: pwd}, function(data){
    window.alert( data);
});

my server code:

app.get( "/reg" , route.reg);
app.post( "/reg", route.doreg);

in route.js:
exports.doreg = function( req ,res){
var msg = req.body;

userOperator.createUser( msg.username , msg.password ,  function( err , user){
   if( err ){
       res.send( {code: 501});
       res.end();
   }else {
       res.send({ code:200, username: user.name , password: user.password});
   }
});
};enter code here

1 Answer 1

2

You are not ending the response if no error is thrown.

else {
       res.send({ code:200, username: user.name , password: user.password});
       res.end(); //missing
}

BTW, the first argument to send() can be a status code:

 if( err ) {
       res.send(501);
 } else {
       res.send(200, username: user.name , password: user.password});
 }
 res.end();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you reply. It still doesn't work. And I find it never step into the callback function in the client side: $.post('myserver' + 'reg', {username : username , password: pwd}, function(data){ window.alert( data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.