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

Commit fd17026

Browse files
committed
#501 Use req.format() to content-negotiate correct response
1 parent ba1a447 commit fd17026

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

modules/core/server/controllers/core.server.controller.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@ exports.renderServerError = function(req, res) {
2020

2121
/**
2222
* Render the server not found responses
23+
* Performs content-negotiation on the Accept HTTP header
2324
*/
2425
exports.renderNotFound = function(req, res) {
25-
res.status(404);
2626

27-
// Respond with html page
28-
if (req.accepts('html')) {
29-
res.render('modules/core/server/views/404', {
30-
url: req.originalUrl
27+
res
28+
.status(404)
29+
.format({
30+
'text/html': function(){
31+
res.render('modules/core/server/views/404', {
32+
url: req.originalUrl
33+
});
34+
},
35+
'application/json': function(){
36+
res.json({ error: 'Path not found' });
37+
},
38+
'default': function(){
39+
res.send('Path not found');
40+
}
3141
});
32-
return;
33-
}
34-
35-
// Respond with json to API calls
36-
if (req.accepts('json')) {
37-
res.json({ error: 'Path not found' });
38-
return;
39-
}
40-
41-
// Default to plain-text
42-
res.type('txt').send('Path not found');
4342
};

0 commit comments

Comments
 (0)