Wednesday, 15 July 2015

error handling - Correct way to respond to client on Node.js? -



error handling - Correct way to respond to client on Node.js? -

this beingness first effort @ building server...

i have set server handle contact form submission includes predefined captcha string.

when server receives contact form, if captcha string 1 expected want respond json of parsed contact form query using response.end(json.stringify(parsedurl));

if captcha string wrong, want server respond "saying" captcha wrong, client asks user seek again. don't know how that.

on server :

var httpserver = http.createserver(function (request, response) { if (/\/contactform\?....../.test(request.url)) { var parsedurl = url.parse(request.url, true); var name = parsedurl.query.name; var email = parsedurl.query.email; var subject = parsedurl.query.subject; var enquiry = parsedurl.query.enquiry; var captcha = parsedurl.query.captcha; if (captcha !== "testing") { // send "bad" response client , include message "bad captcha" } else response.end(json.stringify(parsedurl.query)); } }).listen(8080);

on client :

$.ajax({ url: "/contactform?..............", success: function(msg) { console.log(msg); }, error: function(msg) { // "bad captcha" response should handled here right ? console.log(msg); // should equivalent console.log("bad captcha"); } });

when utilize response.end(json.stringify(parsedurl)); client (jquery) considers "success".

how should respond server "error" part of ajax request on client executed?

or "error" part supposed handle cases server doesn't respond @ all, i.e. when has gone horribly wrong server-side, exception, real error, , not cases evaluation on server doesn't have expected outcome ?

should instead useresponse.end(...); in both cases in :

on server :

var httpserver = http.createserver(function (request, response) { if (/\/contactform\?....../.test(request.url)) { var parsedurl = url.parse(request.url, true); var name = parsedurl.query.name; var email = parsedurl.query.email; var subject = parsedurl.query.subject; var enquiry = parsedurl.query.enquiry; var captcha = parsedurl.query.captcha; var response = json.stringify(parsedurl.query); if (captcha !== "testing") response = "bad captcha"; response.end(response); } }).listen(8080);

on client :

$.ajax({ url: "/contactform?..............", success: function(msg) { console.log(msg); // msg either stringified object or "bad captcha".. } });

in other words, when request received on server server wants allow client know missing or whatever, should response server sent "error" (i.e. handled "error" block on client's ajax code) or "success" appropriate message saying happened?

i think need set headers of response.

here example:

var body = 'sorry!'; response.writehead(404, { 'content-length': body.length, 'content-type': 'text/plain' });

refer http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers more information.

node.js error-handling response server-side

No comments:

Post a Comment