In postman when I make a request to my server I get these response headers:
Connection: keep-alive
Content-Length: 3
Content-Type: text/html; charset=utf-8
Date: Wed, 02 Dec 2020 21:52:10 GMT
ETag: W/"3-f1UKn0xEFzo3Zk2TjxNV8PkqR6c"
Set-Cookie: connect.sid=s%3AHQ54fVZ7KP9jrPKP1j8Z93O8bIXN0CJl.lTSlYwA48TgarO7MtXGFiG1JL%2FPOHe%2FSGV0cqikGGU4;
Path=/; HttpOnly
X-Powered-By: Express
How do I see these headers in express? I paused the server for this request right before res.send('hey')
but all I got was one header:
res.getHeaders()
{x-powered-by: 'Express'}
I specifically want to see the cookie that the server is sending to the browser. How can I see all the response headers in express while debugging?
const express = require('express');
const session = require('express-session');
const app = express();
app.use(
session({
secret: 'very secret 12345',
})
);
app.use(async (req, res, next) => {
req.session.visits = req.session.visits ? req.session.visits + 1 : 1;
console.log(`${req.method}: ${req.path}`);
console.log('req.headers.cookie: ', req.headers.cookie);
next();
});
app.get('/sayhi', (req, res, next) => {
res.send('hey');
});
app.listen(4001).on('listening', () => {
console.log('info', `HTTP server listening on port 4001`);
});
Because Content-Type
Set-Cookie
header is set after you called res.send()
, so you need to add event listener to listen when the response close.
Then you could see what headers this response have.
But, it's a callback function after response sent, so you could just only look what value sent in this response.
app.get('/sayhi', (req, res, next) => {
res.on("close", function() {
console.log("====== close event =======");
console.log(res.getHeaders());
console.log("====== close event =======");
})
res.send('hey');
});
By the way, you could also use DEBUG=* node your_server.js
to look some debug message implemented by other libraries.