TheBrenny
TheBrenny

Reputation: 527

Why does Node expect a WebSocket when using a hostname, but responds normally when using an IP?

As a bit of context: I've been playing around with Windows Terminal, and setting up profiles, keybindings, and, in the case of my Node profile, setting up a script which is required on startup. This js script that gets required gives me a whole bunch of utilities to make me faster in the REPL (defining properties on globalThis as getters, such as clear and pwd). One of these is inspect which opens a new pane in my terminal and should begin inspecting the original Node REPL. Works perfectly... until I changed where Node binds its inspector "server" to.

I also have a tool which I made called hostman which writes to my hosts file with it's goal allowing me to bind my projects to a hostname instead of 127.{1-255}.{1-255}.{1-255} - because that's exactly what hostnames were made for (more or less!). I've done this for my Node REPL: nodejsterm -> 127.99.110.101.

I'll spare the even grosser details, but just know that when I bind and inspect on the IP, everything works as expected, but when I bind and inspect on the hostname, the inspector never connects. I delved through the code of node-inspect to see what was going on and found out that L254-L258 GETs ${host}/json. Naturally, I threw this into Edge to see what would show up.

$ curl http://127.99.110.101:26633/json
[ {
  "description": "node.js instance",
  "devtoolsFrontendUrl": "devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=127.99.110.101:26633/90b76604-50d8-45e5-bda4-3671750a9353",
  "devtoolsFrontendUrlCompat": "devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.99.110.101:26633/90b76604-50d8-45e5-bda4-3671750a9353",
  "faviconUrl": "https://nodejs.org/static/images/favicons/favicon.ico",
  "id": "90b76604-50d8-45e5-bda4-3671750a9353",
  "title": "NodeJS[2424]",
  "type": "node",
  "url": "file://",
  "webSocketDebuggerUrl": "ws://127.99.110.101:26633/90b76604-50d8-45e5-bda4-3671750a9353"
} ]

$ curl http://nodejsterm:26633/json
WebSockets request was expected

I know that there's a lot of writing for one simple question, but what is happening, and why is it happening?

Upvotes: 1

Views: 277

Answers (0)

Related Questions