From fd170261ec9699c99bbceb3a5c5db1e0fab7da48 Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Mon, 18 May 2015 19:22:56 +0300 Subject: [PATCH] #501 Use req.format() to content-negotiate correct response --- .../controllers/core.server.controller.js | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/modules/core/server/controllers/core.server.controller.js b/modules/core/server/controllers/core.server.controller.js index efda2451db..5fe58e65df 100644 --- a/modules/core/server/controllers/core.server.controller.js +++ b/modules/core/server/controllers/core.server.controller.js @@ -20,24 +20,23 @@ exports.renderServerError = function(req, res) { /** * Render the server not found responses + * Performs content-negotiation on the Accept HTTP header */ exports.renderNotFound = function(req, res) { - res.status(404); - // Respond with html page - if (req.accepts('html')) { - res.render('modules/core/server/views/404', { - url: req.originalUrl + res + .status(404) + .format({ + 'text/html': function(){ + res.render('modules/core/server/views/404', { + url: req.originalUrl + }); + }, + 'application/json': function(){ + res.json({ error: 'Path not found' }); + }, + 'default': function(){ + res.send('Path not found'); + } }); - return; - } - - // Respond with json to API calls - if (req.accepts('json')) { - res.json({ error: 'Path not found' }); - return; - } - - // Default to plain-text - res.type('txt').send('Path not found'); };