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
ἔρως
ἔρως2mo ago
how deep into the rabbit hole do you want to go?
Faker
FakerOP2mo ago
hmm just enough to know the basics... then I can learn as I go :c
ἔρως
ἔρως2mo ago
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
Faker
FakerOP2mo ago
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
ἔρως
ἔρως2mo ago
which part you don't understand?
Faker
FakerOP2mo ago
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()
ἔρως
ἔρως2mo ago
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
Faker
FakerOP2mo ago
yep I see one last question, is there a difference between respond.writeHead() and respond.statusCode ? both gives information to the head
ἔρως
ἔρως2mo ago
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
Faker
FakerOP2mo ago
hmmm what about the respond.writeHead()
ἔρως
ἔρως2mo ago
i have no idea never used node to do this you have to read the awful documentation to know what it does
Faker
FakerOP2mo ago
okk never mind, I will gpt it 😅
ἔρως
ἔρως2mo ago
try it
b1mind
b1mind2mo ago
is that the right api? https://developer.mozilla.org/en-US/docs/Web/API/Response/Response This is what you should be using no?
ἔρως
ἔρως2mo ago
it's for node, so, no idea
b1mind
b1mind2mo ago
ah looks like it might be Express method
Faker
FakerOP2mo ago
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?
b1mind
b1mind2mo ago
Its a nodeJS framework I've been seeing lots of love for Hugo replacing Express too
Faker
FakerOP2mo ago
don't know about Hugo, it must be new ig
b1mind
b1mind2mo ago
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
Faker
FakerOP2mo ago
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 😅
Want results from more Discord servers?
Add your server