From a57b86257ce4ac4de3575d4d068d8b7f41eb40d4 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 19 Feb 2016 16:23:00 -0500 Subject: [PATCH] Add content-negotiation --- app/Container.js | 1 + app/config/error-handlers.js | 31 ++++++++++++++++++++++--------- app/controllers/index.js | 8 ++++++-- app/views/error.stache | 3 +++ package.json | 1 + 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/Container.js b/app/Container.js index f1bd158..637d65c 100644 --- a/app/Container.js +++ b/app/Container.js @@ -2,6 +2,7 @@ const errors = require('errors'); const express = require('express'); +const negotiate = require('express-negotiate'); const path = require('path'); const autoLoad = require('./config/container-autoload'); diff --git a/app/config/error-handlers.js b/app/config/error-handlers.js index 162dd65..0cdaa0a 100644 --- a/app/config/error-handlers.js +++ b/app/config/error-handlers.js @@ -8,15 +8,21 @@ const container = require('../Container'); const app = container.get('app'); const HTTP_CODE_MAP = require('http').STATUS_CODES; const errors = require('errors'); +const negotiate = require('express-negotiate'); let errorHandlers = new Set([ - function handle404(req, res, next) { - // if no route matches, send a 404 + function handle400Errors(err, req, res, next) { + if (! req.route) { - let err = new errors.Http404Error(); - return next(err); + // if no route matches, send a 404 + err = new errors.Http404Error(); + } else if (err instanceof negotiate.NotAcceptable) { + // if no content type matches, send a 406 + err = new errors.Http406Error(); } + + return next(err); }, // general error handler @@ -36,12 +42,19 @@ let errorHandlers = new Set([ output.error = err; } - res.render('error', { - title: `${err.status} ${err.message}`, - error: output, - }); + // Send html or json depending on client accept header + req.negotiate({ + html: () => { + res.render('error', { + title: `${err.status} ${err.message}`, + error: output, + }); + }, - //res.json(output); + 'application/json': () => { + res.json(output); + }, + }); }, ]); diff --git a/app/controllers/index.js b/app/controllers/index.js index dfa570f..a87d753 100644 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -4,8 +4,12 @@ module.exports = { '/': { // Get homepage get: (req, res) => { - return res.render('index', { - title: 'Blog test page', + req.negotiate({ + html: () => { + return res.render('index', { + title: 'Blog test page', + }); + }, }); }, }, diff --git a/app/views/error.stache b/app/views/error.stache index b075502..af101d2 100644 --- a/app/views/error.stache +++ b/app/views/error.stache @@ -3,14 +3,17 @@

{{error.message}}

{{#if error.error}}
+
{{#each error as |value key|}} + {{/each}}
Error Details
{{key}} {{value}}
+
{{/if}} \ No newline at end of file diff --git a/package.json b/package.json index 12acccd..52de28b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "eslint": "^1.10.3", "express": "4.*", "express-handlebars": "^3.0.0", + "express-negotiate": "0.0.5", "express-session": "^1.13.0", "getargs": "0.0.8", "glob": "^6.0.4",