Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
#501 Use req.format() to content-negotiate correct response
Browse files Browse the repository at this point in the history
  • Loading branch information
simison committed May 18, 2015
1 parent ba1a447 commit fd17026
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions modules/core/server/controllers/core.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};

0 comments on commit fd17026

Please sign in to comment.