* fix(api): return json for delete + reset-progress * refactor(api): make .json calls explicit These .send() calls had objects as arguments. Using .json() makes it explicit without changing behaviour. * fix: return json or redirectWithFlash We should never be getting requests for plain text, but if we do they should be redirected back to the client. * fix: prioritize JSON responses If accepted, respond with JSON. If not, it's probably a bad request.
20 lines
596 B
JavaScript
20 lines
596 B
JavaScript
import accepts from 'accepts';
|
|
import { getRedirectParams } from '../utils/redirection';
|
|
|
|
export default function fourOhFour(app) {
|
|
app.all('*', function (req, res) {
|
|
const accept = accepts(req);
|
|
// prioritise returning json
|
|
const type = accept.type('json', 'html', 'text');
|
|
const { path } = req;
|
|
const { origin } = getRedirectParams(req);
|
|
|
|
if (type === 'json') {
|
|
return res.status('404').json({ error: 'path not found' });
|
|
} else {
|
|
req.flash('danger', `We couldn't find path ${path}`);
|
|
return res.redirectWithFlash(`${origin}/404`);
|
|
}
|
|
});
|
|
}
|