mirror of
https://github.com/expressjs/express.git
synced 2026-02-21 19:41:36 +00:00
Since v5 relies on node >= 18, this is now possible (since v16, v14.18.0 [^1][^2]). It's functionally irrelevant: 1. It's not required for CJS nor ESM (with a few exceptions [^3]) 2. It has no performance promises However, there are upsides to this approach: 1. It brings clear boundaries to what's a built-in and what's an external dependency 2. It reduces the risk of importing unwanted deps where a built-in is expected 3. It's slightly more interoperable with other JS runtimes that provide node compatibility[^4], albeit only during development. Once imported from npm, built-ins are assumed. [^1]:https://nodejs.org/docs/latest-v22.x/api/modules.html#built-in-modules [^2]:https://github.com/nodejs/node/pull/37246 [^3]:https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix [^4]:https://docs.deno.com/runtime/fundamentals/node/#using-node's-built-in-modules
104 lines
2.6 KiB
JavaScript
104 lines
2.6 KiB
JavaScript
'use strict'
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
var express = require('../../');
|
|
var path = require('node:path');
|
|
var app = module.exports = express();
|
|
var logger = require('morgan');
|
|
var silent = process.env.NODE_ENV === 'test'
|
|
|
|
// general config
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'ejs');
|
|
|
|
// our custom "verbose errors" setting
|
|
// which we can use in the templates
|
|
// via settings['verbose errors']
|
|
app.enable('verbose errors');
|
|
|
|
// disable them in production
|
|
// use $ NODE_ENV=production node examples/error-pages
|
|
if (app.settings.env === 'production') app.disable('verbose errors')
|
|
|
|
silent || app.use(logger('dev'));
|
|
|
|
// Routes
|
|
|
|
app.get('/', function(req, res){
|
|
res.render('index.ejs');
|
|
});
|
|
|
|
app.get('/404', function(req, res, next){
|
|
// trigger a 404 since no other middleware
|
|
// will match /404 after this one, and we're not
|
|
// responding here
|
|
next();
|
|
});
|
|
|
|
app.get('/403', function(req, res, next){
|
|
// trigger a 403 error
|
|
var err = new Error('not allowed!');
|
|
err.status = 403;
|
|
next(err);
|
|
});
|
|
|
|
app.get('/500', function(req, res, next){
|
|
// trigger a generic (500) error
|
|
next(new Error('keyboard cat!'));
|
|
});
|
|
|
|
// Error handlers
|
|
|
|
// Since this is the last non-error-handling
|
|
// middleware use()d, we assume 404, as nothing else
|
|
// responded.
|
|
|
|
// $ curl http://localhost:3000/notfound
|
|
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
|
|
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
|
|
|
|
app.use(function(req, res, next){
|
|
res.status(404);
|
|
|
|
res.format({
|
|
html: function () {
|
|
res.render('404', { url: req.url })
|
|
},
|
|
json: function () {
|
|
res.json({ error: 'Not found' })
|
|
},
|
|
default: function () {
|
|
res.type('txt').send('Not found')
|
|
}
|
|
})
|
|
});
|
|
|
|
// error-handling middleware, take the same form
|
|
// as regular middleware, however they require an
|
|
// arity of 4, aka the signature (err, req, res, next).
|
|
// when connect has an error, it will invoke ONLY error-handling
|
|
// middleware.
|
|
|
|
// If we were to next() here any remaining non-error-handling
|
|
// middleware would then be executed, or if we next(err) to
|
|
// continue passing the error, only error-handling middleware
|
|
// would remain being executed, however here
|
|
// we simply respond with an error page.
|
|
|
|
app.use(function(err, req, res, next){
|
|
// we may use properties of the error object
|
|
// here and next(err) appropriately, or if
|
|
// we possibly recovered from the error, simply next().
|
|
res.status(err.status || 500);
|
|
res.render('500', { error: err });
|
|
});
|
|
|
|
/* istanbul ignore next */
|
|
if (!module.parent) {
|
|
app.listen(3000);
|
|
console.log('Express started on port 3000');
|
|
}
|