My Deno app is using the node:http
package to create a server. I have noticed that the behaviour of this package seems to differ between Deno and Node, and that it does not comply with the documentation. Specifically, the value of Request.rawHeaders
is not as I expect it to be.
I used the following code to inspect the value of Request.rawHeaders
.
import * as http from "node:http"
createServer((req, res) => {
// My console.logs here
res.end()
}).listen(80)
Similar code was used in Node to test the same value. After making a request to both servers, I observed the following results.
Value passed to console.log |
Console result in Node | Console result in Deno |
---|---|---|
req.rawHeaders |
[‘Host’, ‘localhost’, ‘Connection’, ‘keep-alive’, …] | |
req.rawHeaders.toString() |
Host,localhost,Connection,keep-alive, … | [object Headers] |
JSON.stringify(req.rawHeaders) |
[“Host”,”localhost”,”Connection”,”keep-alive”, …] | {} |
typeof req.rawHeaders |
object | object |
Object.keys(req.rawHeaders) |
[‘0’, ‘1’, ‘2’, ‘3’, …] | [] |
Object.getOwnPropertyNames(req.rawHeaders) |
[‘0’, ‘1’, ‘2’, ‘3’, …] | [] |
Array.isArray(req.rawHeaders) |
true | false |
Object.getPrototypeOf(req.rawHeaders) |
Object(0) [] | Headers {} |
According to the docs on rawHeaders, the value should be an array of strings. It is not in Deno. I can’t even understand what it is. A direct console.log
on the value shows that it is an object with properties, but I can’t access them. I tried rawHeaders['host']
and rawHeaders.host
. They both return undefined
. How is this possible? What is console.log
doing to access the values?
What am I doing wrong? Is this a bug?
Additional details
I discovered this while trying to use vhost
in Deno. I noticed that it does not redirect to the correct handler based on the subdomain, presumably because it can’t read the host
header. I’m using express
instead of oak
because oak
requires too much boilerplate for my current requirements. Also, I don’t know if I can use vhost
with oak
.