How to get a list of HTTP response headers currently installed in Node / Express?

As I understand it, when you build an HTTP response in node / express or something else, the process consists primarily of two inconsistent steps: defining the headers and building the body. Headers include Set-Cookie headers. In Express, the following methods are available for the response object for setting headers:

res.append(); // To append/create headers res.cookie(); // A convenience method to append set-cookie headers. 

Since the headers are only buffered and not actually sent until the response is sent, is there any method or mechanism for getting the current list of header sets along with their values, for example:

  headers = res.getHeaders(); //Returns an object with headers and values headers = res.getHeaders('Set-Cookie'); // To get only select headers 
+7
source share
2 answers

try

 console.log("res._headers >>>>>>>" + JSON.stringify(res._headers)); 
+7
source

I was able to verify what is being sent (including cookies) using response.getHeaders() (available from node 7.7.0) in conjunction with the on-headers module . Like that:

 import express from 'express' import onHeaders from 'on-headers' const router = express.Router() function responseDebugger() { console.log(JSON.stringify(this.getHeaders())) } router.post('/', (req, res, next) => { onHeaders(res, responseDebugger) res.json({}) }) export default router 
0
source

All Articles