Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express/node.js — req.getHeader is not a function

I'm learning node.js/express and I'm trying to get the Date value from the header and send it to the front-end.

I can't seem to use req.getHeader() to do this. I'm also trying to console.log(req) and console.log(req.headers) but the output is different than the headers in postman.

This is what console.log(req.headers) gives me:

{
  'user-agent': 'PostmanRuntime/7.28.0',
  accept: '*/*',
  'cache-control': 'no-cache',
  'postman-token': '6d5acbf2-b63d-46d4-b189-86e163281421',
  host: 'localhost:3001',
  'accept-encoding': 'gzip, deflate, br',
  connection: 'keep-alive'
}

Which is different from what postman headers are:

postman

Here is my code:

const express = require("express");
const app = express();
const cors = require("cors");
const PORT = process.env.PORT || 3001;

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

let persons = [
  {
    id: 1,
    name: "Arto Hellas",
    number: "040-123456",
  },
  {
    id: 2,
    name: "Ada Lovelace",
    number: "39-44-5323523",
  },
  {
    id: 3,
    name: "Dan Abramov",
    number: "12-43-234345",
  },
  {
    id: 4,
    name: "Mary Poppendieck",
    number: "39-23-6423122",
  },
];

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

app.get("/api/persons", (req, res) => {
  res.json(persons);
});

app.get("/api/persons/:id", (req, res) => {
  const id = Number(req.params.id);
  const person = persons.find((person) => person.id === id);
  const unknown = persons.id == "unknown";

  if (person) {
    res.json(person);
  } else {
    res.status(404).end();
  }
});

app.get("/info", (req, res) => {
  let count = persons.length;
  let str = `Phonebook has info for ${count} people.`;

  console.log(req);
  console.log(req.headers);

  let timeDate = req.getHeader(Date); // <-- here is the problem
  console.log(timeDate);
  res.send(`<p>${str} \n ${timeDate}</p>`);
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
like image 583
Julian Avatar asked Apr 27 '26 00:04

Julian


1 Answers

First off, if you're using Express, then it's better to use:

req.get("someHeader")

as this normalizes a number of things (like case sensitivity and interchangeable header names).

Then, you must pass it a String as in:

req.get("Date");

Note that incoming http requests from the browser do NOT contain a Date header. So, there will only be such a header if you have some sort of custom client that is adding that header.

Your req.getHeader() is not working because such a method does not exist for an incoming http message object. This is a bit confusing in the nodejs docs, but you are looking at the http.ClientRequest object. That is not what you get as the req parameter for an incoming http request. You get an http.IncomingMessage object which is documented here and does not have a req.getHeader(). It has req.headers which gives you the whole object of headers which you can then access the one you want.

Or, just use req.get("Date") that Express provides which gives you case insensitive access. Keep in mind that incoming http requests from a browser do not have a Date header so for your server to use that header, it will have to be from a client that adds that header to its request. Responses from your server back to the browser will have a Date header.


The object in the doc you were probably looking at that does have a req.getHeader() method is a http.ClientRequest object which is what you get back from http.get() and it refers to an outgoing http request from a client, not an incoming http request on your server.

Yes, this is a bit confusing since all the examples tend to use the same req variable name, yet different uses produce different objects for that variable.

like image 62
jfriend00 Avatar answered May 01 '26 07:05

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!