This article describes the exception handling operation of Nodejs. Share it for your reference, as follows:
= { expfun: function(flag) { if(flag == 0) { throw 'I'm an error'; } return "success"; } }
//-------------------------------------- var fs = require('fs'); = { readfile: function (path, recall) { //Asynchronous execution (path, function (err, data) { if (err) { ("Async execution error:" + err); recall("The file does not exist, asynchronously execute error:" + err);//Asynchronous processing of exceptions } else { //(()); recall(data); } }); ("===The asynchronous method has been executed==="); }, readImg: function (path, res) { (path, 'binary', function (err, filedata) { if (err) { (err); return; } else { ("Output File"); (filedata, 'binary'); (); } }); } }
var optfile = require('../model/'); function getRecall(req, res) { (200, { 'Content-Type': 'text/html; charset=utf-8' }); function recall(data) { (data); (''); //If you don't write, there is no end to http protocol } return recall; } = { login: function (req, res) { recall = getRecall(req, res); ('./view/', recall); }, showimg: function (req, res) { (200, { 'Content-Type': 'image/jpeg' }); ("./view/", res); } }
//-------------n9_exception.js--------------- /* Synchronous Capture &&Async Capture */ var http = require('http'); var url = require('url'); var router = require('./model/router'); var exception = require('./model/Exception'); (function (request, response) { if ( !== "/") { //Clear this second access pathname = ().pathname; pathname = (/\//, ''); //Replace the previous one/ try { router[pathname](request, response); // data = (0); // (data); // (''); } catch (err) { ('Catched exception=' + err); (200, { 'Content-Type': 'text/html; charset=utf-8' }); (()); (''); } ("server execution completed"); } }).listen(8000); ('Server running at http://127.0.0.1:8000/');
I hope this article will be helpful to everyone's nodejs programming.