There is more to node http server listen
Skills:
Backend Performance80%
Key Takeaways
Exploring different ways to listen on an HTTP server in Node.js using the http module
Full Transcript
i've been recently doing this series where i pick one small piece of the node.js platform or the javascript platform and then just pick it apart a very low level stuff i talked about fetch i talked about http module and today i'm going to talk about the http server listening in node.js and then just take it apart what is it doing actually right how about we actually just jump into it guys i'm gonna create a brand new javascript file let's call it listen.js and uh we'll just go ahead and create a new http variable here we're gonna require the http server and we'll create an http server object by calling http.createserver and nothing more just just that okay and we're used to really just calling listen and providing a port but i want to you know explore what other options we have here and what do they mean really and uh when we actually provide a port we're gonna listen on that port obviously and we're gonna tell us that but uh i don't know if many of you know that even the poor there is a question mark next to it which means it's actually optional so technically this works and the first question that you might have is saying if it's gonna listen where is it going to listen to which port well everything is an event almost in in node.js so you can effectively have an on listening and like a visual studio code actually autocompletes for you and it tells you hey in on listening call this function right this is very similar to when you provide the callback in the listen argument but what we're going to do here is effectively hey when when i am listening each can just literally just print hey listening on port and here's the beauty you can get the port from the http server new we need to be really careful here because this will only be called when the listen event is successful and basically if you don't provide a port or you provided zero as a value for the port since zero is not a valid port it will pick a random pore and listen to it and uh here's how you get that port that random port that has been picked for you and you can do http server i just found out this really recently to be honest while i was exploring and making the other video [Music] you can do http server.address an address is a function right a synchronous function that you can call and it's going to give you a lot of useful information part of them is a port and uh if i do that now and uh let's just let's just uh because since this is really use useless app so let's add an actual event here on request right if someone make any requests to us i wonder if you can do an end and write something i'm not quite sure let's try that uh let's just do okay okay okay and let's just do run follows let's go ahead and run you can see that we were picked the port 57565 that means if i go to if i go to a browser and type in http localhost you can see you had our servers beautifully running and you can do tricks like that and your app says like on a localhost then you can put the port here and what's his name virtual studio code will actually give you almost a link when you can do com command click and it will open for you right it will all different definitely pick a different port this is this is useful uh specifically if you if you don't know the environment you're running in and you have no idea what ports are gonna be reserved right and other apps are listening to you can use this approach but this is not really the fascinating part to me uh what what is really interesting here is there's another parameter here is like on i'm going to type in this like what are we listening to is the other question that is actually the most dangerous question here sometimes there's an address right you would say i'm saying you're listening on your machine of course you can listen to your address you know your eye address not really it's not as clean cut and dry really i wanted to i wanted to show you this most of you some some of you might know but this colon colon is the short hand writing for the ipv6 uh uh let's write it here colon cone is equal to 0 0 0 in ipv4 this is the ipv6 and i believe which this this is an invalid it's called i think an unspecified unspecific address and which means really it's an invalid address you cannot use this physically on the network but software can and usually what it means it means every interface so like if my if i had a wi-fi and i have a 5g network and i have another public lan ethernet my code by default listens to all of them and you really need to be very critical and very very careful with this so technically i am running it through localhost but so if i open this again and i open the new one knighted hussein mac dot local this is my dns which technically points to my ip address right that also works my ip address is this that works so i was able to go through localhost i was able to go through my dns host name and i was able to go through my ipv4 and i'm pretty sure if i have an ipv6 i can do that too i just couldn't figure out how to actually paste the ipv6 here and then have it work from the browser so that means that if i have my phone and i'm going to show you right now if i type in on my phone here hussein mac dot local colon 575 89 that works from my phone on the same network you might say i'm saying duh so yeah we've shown that it works on my phone it works everywhere but just just the defaults is listening in everywhere and and i've seen some apps incorrectly do that and the danger of this is if your app is supposed to be like an administrative only that is only should be accessible from localhost and not any other interfaces you really should only access from the same machine then you only want to listen on the loopback or certain ip addresses right if you accidentally deployed your node.js app and that goes for any application really this is almost always the default if you accidentally deployed this on the cloud oh no on some on something that has a public ip address then anyone scanning the internet for this open port they will be able to access you why do you think we have all these data leaks that is happening with elasticsearch and mongodb because people just listen on all interfaces even though they don't necessarily want to do that you only want to listen to the interfaces that you absolutely are only needing to connect to like in a development environment if you're the only one here you don't want anyone in in in the vicinity in the same network at be able to access your stuff the app that you just wrote because it could be dangerous you might say i don't care and that's fine just understanding this is actually i think i think it's important all right if i said again zero for the port and you can specify the hostname says hey i really not in only interested in uh 127.0.0.1 i only want to be listening on the loopback and not any loopback i want to listen on the ipv4 loopback which is one type of seven zero zero that means let's run again boom this works because it's a localhost this also works but this right what's my ipad is again 192.168.254.10 doesn't work when your client which is the browser tried to establish a tcp connection to this ip address the operating systems past that sin try to pass this into this interface and says wait a second nobody's listening on this part on this particular ip address right that's why the connection was refused you didn't get any synack as a result and sometimes this is a good thing if you don't want anyone especially like i don't know if you are liking a public wi-fi right building those apps when you listen you only want to listen on this things that you absolutely need you know just another another thing and then um i'll add that there is some other useful information which is here tells you what family right here which is the family of the ip address tells you okay ipv4 we know that 127 is ipv4 if you want to listen to the like let's say you have an i uh ipv6 configuration i believe it's this right this is the localhost version of ipv6 i might be wrong we're gonna look it up if that's wrong there you go this is actually correct zero zero one now i have no clue to be honest how to actually browse this right here is it just pasting this i really i don't think this will work exactly so it doesn't work i don't know how to navigate the uh ipv6 let's let's actually look it up thank you internet it's a square bracket you have to add a square bracket and then you put your ip v6 and then you can connect to it so that's how you connect to an ipv6 right let's see if actually 127.0201 works and this is what i expected 127.0.01 doesn't work because well you only listen to the ipv6 you clearly listen to only the ipv6 version right so i wonder what localhost will do to be honest will it listen to both ipv6 and ipv4 it sounds like it defaulted to ipv4 but let's find out uh wrong wrong port probably it's not convenient to generate a random port every time huh no i just realized that it's convenient to listen to the same part it's just something new guys all right okay now ipv4 is listening but ipv6 is not it's it's interesting to kind of pick up all these small things and all right let's do some more fancy stuff what i'm gonna do is i am going to listen to a random port but i'm going to listen to the loop back ipv4 and then i'm going to create a brand new server i'm going to call it ipv6 i'll call it six here paste paste paste uh paste paste paste okay and here i am going to listen to the dit one so now i'm gonna get two different ports right and i'm listening on two different interfaces right let's do that oh forgot the six here now i'm gonna just like listening server two i'm gonna server six call it here and server four which means like ipv4 what was gonna do now what happens we're experimenting as we're playing here look at that we got obviously two different ports they are after the other but uh just let's fix this too man we know how to connect to ipv6 now so let's do that so now this is boom let's open that up and then let's boom let's open that up so technically both of them are working but both of them are completely different servers so now instead of saying hey okay you are hitting the ipv6 server and this guy okay you are hitting ipv4 server look at all this stuff that we're seeing here refresh and now i'm gonna do this boom and then boom look at that ipv4 server ipv6 server now i'm going to ask you a question what if i de-randomized this thing eight eight eight eight eight eight eight eight what do you think is gonna happen who can make a guess am i gonna get an error or not thank you guys no errors guys of course why because the uniqueness of the port is address port pair okay it's not just the port right you're listening on the local host so technically you can also listen on 888 on the ipv4 public public private ipad is 192. right that's that gives you another interface if you want to scroll with someone you can definitely do that so let's see what happened now boom open that guy up so it's six hit that up oh localhost localhost is pointing look at that localhost actually pointed to bv6 so we actually need to do boom boom one right we have to do 127.0.0.1 because the localhost the dns is effectively pointing by default to the ipv6 you know that's another thing you need to understand localhost is just a dns label right i don't know if that's actually a correct thing to say but that label point has to point to the ip address by default it sounds like it's pointing to the ipv6 right so technically to be more accurate this thing has to be 127.0 capisce good stuff open that up look at that it's a completely different server let's close all this taps so now if i actually forced an error let's force an error by doing this this is a no no because now you're listening to this guy and you're listening in the same interface right so if you refresh process exited with code one and we have no idea why so now this is when we actually need to somehow capture the errors which we didn't by the way right that's why when you need that dot on error effectively and i love visual studio code look at that the people behind this visual studio code are heroes so we're going to do a console.log goddess i'm going to do an error what am i doing error error and i have no idea what i think this is just a string but it might have been an object and we do yy and then i go capital p wrong undo i'm still figuring out them although i converted everything to them here still trying to learn them learn and just like force myself to you know i'm getting used to it but i only know very few commands that you know get me up and running but i'm no expert now let's go ahead and re-run this thing and we're not getting any or what whoa nani server for listening so we listened successfully on this guy what oh oh oh gonna do an error we forgot that how did i forget this but even though we should have gotten something right that's odd there you go we got an error error error listen address in use address already in use by address they mean the pair of ip uh port right you you can get the same error if you like uh stop this if you do like zero zero zero zero which means all interfaces and if you do the same thing here you're gonna get the same error and probably it's better to do an actual error huh so we can see it in red see it in red look at that although zero zero zero means all interfaces right but uh it also means that you already listened on this particular ip address which means all interfaces means sorry i can't do that or and already someone else is listening so yeah guys uh i think i think i'm gonna stop here and let's let's get back to uh just do let's have this undo let's do this to be back to the ipv6 and this guy to be to be ivf another refresh yep the stop here uh i think i think it's fun to kind of understand these basic fundamentals and this is just from one command you know there is so much we can scratch the service and learn and i don't know i personally find it fascinating i don't know if you do but i just like to talk about this low-level stuff gonna see you in the next one you guys stay awesome goodbye
Original Description
In this video, I explore the different ways you can listen on an HTTP server on node js.
0:00 Intro
2:00 Listening Event
3:00 Port 0
5:00 Address
9:00 Listening to 127.0.0.1 ipv4 only
12:00 Listening to ::1 ipv6 only
14:00 Two Servers One Port
18:00 Handling Errors
Code
https://github.com/hnasr/javascript_playground/tree/master/http-module
Become a Member on YouTube
https://www.youtube.com/channel/UC_ML5xP23TOWKUcc-oAE_Eg/join
🔥 Members Only Content
https://www.youtube.com/playlist?list=UUMO_ML5xP23TOWKUcc-oAE_Eg
Support my work on PayPal
https://bit.ly/33ENps4
🧑🏫 Courses I Teach
https://husseinnasser.com/courses
🏭 Backend Engineering Videos in Order
https://backend.husseinnasser.com
💾 Database Engineering Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQXjD0HOzN7P2tgzu7scWpl2
🎙️Listen to the Backend Engineering Podcast
https://husseinnasser.com/podcast
Gears and tools used on the Channel (affiliates)
🖼️ Slides and Thumbnail Design
Canva
https://partner.canva.com/c/2766475/647168/10068
🎙️ Mic Gear
Shure SM7B Cardioid Dynamic Microphone
https://amzn.to/3o1NiBi
Cloudlifter
https://amzn.to/2RAeyLo
XLR cables
https://amzn.to/3tvMJRu
Focusrite Audio Interface
https://amzn.to/3f2vjGY
📷 Camera Gear
Canon M50 Mark II
https://amzn.to/3o2ed0c
Micro HDMI to HDMI
https://amzn.to/3uwCxK3
Video capture card
https://amzn.to/3f34pyD
AC Wall for constant power
https://amzn.to/3eueoxP
Stay Awesome,
Hussein
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Hussein Nasser · Hussein Nasser · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
Channel Update - New Book, New Job, New Videos
Hussein Nasser
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
Javascript by Example - The Vook
Hussein Nasser
Vlog - Keep your servers close and your database closer
Hussein Nasser
Vlog - Client/Server Programming Languages
Hussein Nasser
Javascript By Example L1E01 - Getting Started
Hussein Nasser
Persistent Connections (Pros and Cons)
Hussein Nasser
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
Happy new Year from IGeometry!
Hussein Nasser
Synchronous v. Asynchronous
Hussein Nasser
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
Relational Database Atomicity Explained By Example
Hussein Nasser
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
What Comes First, User Experience or Software Architecture?
Hussein Nasser
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
Advice for New Software Engineers and Developers
Hussein Nasser
Why JSON is so Popular?
Hussein Nasser
Building Scalable Software - SLA, HS, VS
Hussein Nasser
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
Learn By Doing.
Hussein Nasser
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
My Story
Hussein Nasser
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
Can User Experience Help Build Better Rest API?
Hussein Nasser
Reverse engineering Instagram in flight mode
Hussein Nasser
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
The evolution from virtual machines to containers
Hussein Nasser
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
Canary Deployment (Explained by Example)
Hussein Nasser
No Excuses
Hussein Nasser
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
What is an Asynchronous service?
Hussein Nasser
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
Nodejs Express "Hello, World"
Hussein Nasser
Reverse Engineering Instagram feed
Hussein Nasser
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser
More on: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
10 Most Common Mistakes Java Developers Make in Interviews
Medium · Programming
# C++ Error Messages Translated — 10 Common Compilation & Link Errors Explained
Dev.to · Yilong Wu
# Picking What to Read Next: The Trade-offs of Ranked-Choice Voting in a Django App
Medium · Python
The Ultimate Rust ORM Comparison 2026: Diesel vs SQLx vs SeaORM vs Rusqlite — Pick Your Powerhouse!
Medium · Programming
Chapters (8)
Intro
2:00
Listening Event
3:00
Port 0
5:00
Address
9:00
Listening to 127.0.0.1 ipv4 only
12:00
Listening to ::1 ipv6 only
14:00
Two Servers One Port
18:00
Handling Errors
🎓
Tutor Explanation
DeepCamp AI