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
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();