How does HTTP headers/HTTP request and respond work
Hello guys, sorry to disturb you all; I'm trying to build a small server using nodejs but first I need to understand how http and http headers work; can someone explain please. All I understand that our headers contain a request header and response header but it's unclear
21 Replies
how deep into the rabbit hole do you want to go?
hmm just enough to know the basics... then I can learn as I go :c
well, your server listens to the port 80 (http) and 443 (https)
magic happens and connection stuff happens and you receive data
now, if it is http 1.1 or 1.0, you receive more human readable headers
if it is http 2.0 or 3.0, then it's a binary protocol
from then on, the request is parsed by the http part, and more magic stuff happens based on the headers sent
then, you can set headers to send information back to the browser, and the body of the response goes to the client
yep I see, the thing I'm confuse about is the headers
how does it work, I know there are multiple type of headers, reques headers and respond headers... I notice all the code written for a server is based on the headers
which part you don't understand?
hmmm like what is the content-type used for and when does it occur, before the request or after a respond is received
what is the purpose of writing to a response like using response.writeHead() and using response.end()
content-type occurs always
without it, the browser can't know what's being sent
and the server can't know what it is receiving and how to parse it
you can't send headers if you send content
headers go at the top, content goes after
if you're in the middle of writting the html, the headers (probably) have already been sent to the browser, so, you can't do anything else
you must separate when the headers happen and where the content happens, so the browser also knows what is headers and content
yep I see
one last question, is there a difference between respond.writeHead() and respond.statusCode ?
both gives information to the head
status code is a specific number to indicate the status of the response
for example, 404 means "not found" - whatever was requested wasn't found anywhere
there's also 200 ok - it means everything is fine and there were no errors
you should check the entire list of http status codes
hmmm what about the respond.writeHead()
i have no idea
never used node to do this
you have to read the awful documentation to know what it does
okk never mind, I will gpt it 😅
try it
is that the right api?
https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
This is what you should be using no?
it's for node, so, no idea
ah looks like it might be Express method
hmmm I don't know, in the tutorial I'm looking they used that
I will have to learn express also though
how does express facilitates the creation of a server?
Its a nodeJS framework
I've been seeing lots of love for Hugo replacing Express too
don't know about Hugo, it must be new ig
SvelteKit uses Polka 🥲 (or atleast it used too 🤔 ). Sorry I'm derailing, just like to point out there is alternatives.
https://github.com/lukeed/polka
Yeah I need to learn about express I have no choice, it is part of the uni requirement 😭
it's okay, always good to know new things 😅