2022-02-02 01:30:51 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
2010-07-13 08:48:44 -07:00
|
|
|
/**
|
|
|
|
|
* Module dependencies.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-03-27 09:15:27 -07:00
|
|
|
var express = require('../../');
|
|
|
|
|
var logger = require('morgan');
|
|
|
|
|
var app = module.exports = express();
|
2018-01-21 20:14:47 -05:00
|
|
|
var test = app.get('env') === 'test'
|
2010-07-13 08:48:44 -07:00
|
|
|
|
2014-02-15 20:20:12 -05:00
|
|
|
if (!test) app.use(logger('dev'));
|
2011-11-20 12:03:45 -08:00
|
|
|
|
|
|
|
|
// error handling middleware have an arity of 4
|
|
|
|
|
// instead of the typical (req, res, next),
|
|
|
|
|
// otherwise they behave exactly like regular
|
|
|
|
|
// middleware, you may have several of them,
|
|
|
|
|
// in different orders etc.
|
|
|
|
|
|
|
|
|
|
function error(err, req, res, next) {
|
|
|
|
|
// log it
|
2012-04-26 04:56:23 -07:00
|
|
|
if (!test) console.error(err.stack);
|
2011-11-20 12:03:45 -08:00
|
|
|
|
|
|
|
|
// respond with 500 "Internal Server Error".
|
2014-08-06 18:13:54 -04:00
|
|
|
res.status(500);
|
|
|
|
|
res.send('Internal Server Error');
|
2011-11-20 12:03:45 -08:00
|
|
|
}
|
2010-07-13 08:48:44 -07:00
|
|
|
|
2023-02-03 20:34:39 +05:30
|
|
|
app.get('/', function () {
|
2010-11-25 13:41:50 -08:00
|
|
|
// Caught and passed down to the errorHandler middleware
|
|
|
|
|
throw new Error('something broke!');
|
2010-07-13 08:48:44 -07:00
|
|
|
});
|
|
|
|
|
|
2010-07-26 13:36:21 -07:00
|
|
|
app.get('/next', function(req, res, next){
|
2010-11-25 13:41:50 -08:00
|
|
|
// We can also pass exceptions to next()
|
2016-02-25 07:46:14 +05:00
|
|
|
// The reason for process.nextTick() is to show that
|
|
|
|
|
// next() can be called inside an async operation,
|
|
|
|
|
// in real life it can be a DB read or HTTP request.
|
2011-11-20 12:03:45 -08:00
|
|
|
process.nextTick(function(){
|
|
|
|
|
next(new Error('oh no!'));
|
|
|
|
|
});
|
2010-07-13 08:48:44 -07:00
|
|
|
});
|
|
|
|
|
|
2014-01-25 17:57:25 -05:00
|
|
|
// the error handler is placed after routes
|
|
|
|
|
// if it were above it would not receive errors
|
|
|
|
|
// from app.get() etc
|
|
|
|
|
app.use(error);
|
|
|
|
|
|
2014-05-28 00:07:27 -04:00
|
|
|
/* istanbul ignore next */
|
2011-12-18 17:27:13 +01:00
|
|
|
if (!module.parent) {
|
|
|
|
|
app.listen(3000);
|
|
|
|
|
console.log('Express started on port 3000');
|
2014-01-25 17:57:25 -05:00
|
|
|
}
|