A stream can send only one type of content for a request. However, depending on your Accept headers, you can send different content for different requests on the same request URL
app.get('/', function (req, res) {
if(req.accepts('text/html')){
res.sendfile(__dirname + '/index.html');
return;
}
else if(req.accepts('application/json')){
res.json({'key':'value'});
return;
}
});
Here if your request header accepts 'text/html'
, it will return the index.html file. and if the request header accepts 'application/json'
it will return the JSON response.