javascript - How to make route redirect or send JSON back depending on accept headers? -
i want send json if user accesses route , accept headers allow json, , want redirect user page if user accesses route , accept headers not allow json.
my solution hacky, involves inspecting req.headers.accept , seeing whether string contains json. if does, return json, else, redirect. there more optimal solution?
you can try res.format
method.
res.format({ 'application/json': function(){ res.send({ message: 'hey' }); }, default: function(){ res.redirect('nojson.html'); } });
Comments
Post a Comment