I just started a project using Express and I have no real knowledge in node.js, and even in Javascript in general. I was wondering what was the proper way of throwing and catching errors so that they display correctly, and also so that I don't duplicate too much code. I thought this bit of code in app.js:
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
acted like a big catch that would intercept all the errors I would throw in my controllers. But I don't really know how to make it work. For example, let's take this bit of code in users.js:
/* GET users listing. */
router.get('/', function(req, res, next) {
connection.query('SELECT id, name FROM user', function(err, rows, fields) {
if (err) throw err;
res.render('users', {
title: 'Users',
userList: rows
});
});
});
Any help/hint would be appreciated, I'll keep looking on forums and stuff in the meantime.
Express uses a series of middleware to handle the request. If the route matches and defined route it will handle it. Otherwise, you can put '/404' route at last after importing all the routes. It will automatically reach there when no route is found. Else if you don't want to send a response you can use next() so that it moves to next middleware and ultimately to '/404' middleware.
/* GET users listing. */
router.get('/', function(req, res, next) {
connection.query('SELECT id, name FROM user', function(err, rows, fields) {
if (err) {
return res.status(502).json({message:'db error'});
};
res.render('users', {
title: 'Users',
userList: rows
});
});
});
Thank you all for your answers. What I was looking for (and found) was the next(error) function to pass control to the error handler located in app.js (Cf. http://expressjs.com/en/guide/routing.html for more details).
In Express, each route handler is passed a 'res' parameter, that is used to provide the response to the client. Sample usage of creating a response with a specific code, code error and empty body
router.get('/', function(req, res, next) {
res.status(401, "Authentication mismatch").json({});
});
Instead of json() you can also use send() to send a plain text back.
You can give a relevant message with statusCode like this
/* GET users listing. */
router.get('/', function(req, res, next) {
connection.query('SELECT id, name FROM user', function(err, rows, fields) {
if (err){
res.status(500).send("Something is not right");
};
//if you get rows of array type, then you can do this for empty records
else if(rows.length===0){
res.status(204).send('No records found!')
}else{
res.render('users', {
title: 'Users',
userList: rows
});
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.