UNPKG

webappengine

Version:

A web application server that can host multiple web apps running with Node.js.

46 lines (38 loc) 893 B
'use strict'; /** * errnotfound: * * Examples: * * app.use(middleware.errnotfound({ view: '404', error: 'Not found' })) * * Options: * * - view view * - error error message * * @param {Object} options * @return {Function} * @api public */ var errnotfound = function errnotfound(options) { options = options || {}; var view = options.view || '404', error = options.error || ''; return function (req, res, next) { res.status(404); // respond with html page if (req.accepts('html')) { res.render(view, { url: req.url }); return; } // respond with json if (req.accepts('json')) { res.send({ error: error }); return; } // default to plain-text. send() res.type('txt').send(error); }; }; module.exports = errnotfound;