11-01 00:04
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Web App/Node.js Express] Error Handling 본문

Programming/Web App

[Web App/Node.js Express] Error Handling

cinema4dr12 2014. 9. 9. 21:33

[Basic Error Handling]

// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); });


[For Error Status - 404]

app.use(function(req, res){ res.send(404, "404 - Not Found"); }); app.use(function(err, req, res, next){ res.status(err.status || 404); res.end(err.message); });


[For Error Status - 404 - Result]



[Username - Code]

var app = express(); app.get('/users/:username', function(req, res, next){ res.send(req.params.username + "'s profile"); }); app.param('username', function(req, res, next, username){ if(username != 'gchoi') { req.user = username; next(); } else { next(new Error("No User Found")); } });


[Username - Results]



Comments