I saw some code examples about using express res.get(field)
Is it for debugging purpose or there are other use cases for it?
the code examples I saw looks like this :
var express = require('express');
var app = express();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
// Setting the Content-type
res.set({
'Content-Type': 'application/json',
});
// "text/plain"
console.log(res.get('Content-Type'));
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
when hitting the end point the output :
application/json; charset=utf-8
CodePudding user response:
Express uses this method internally to check if particular headers are set, typically to set a default if they aren't or to augment them (for instance, in your example you set the Content-Type
header to application/json
, and Express appended ; charset=utf-8
to it).
You could imagine situations where a request handler wants to see if a particular middleware (perhaps a third-party one) set a particular header and/or a particular value, and adjust the way it handles the final response.