How to Build a Web Server with Node JS | Node.js Tutorials for Beginners

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ค. 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    Learn how to build a web server with Node JS. This Node.js tutorial for Beginners will walk you through building a web server with only Node JS - no framework required. This will help you build foundational knowledge of Node.js which will help when the Express JS framework is introduced later in this Node JS tutorial series.
    🚩 Subscribe ➜ bit.ly/3nGHmNn
    🚀 This lesson is part of a Node.js for Beginners tutorial series playlist:
    • Node.js Tutorials for ...
    🔗 Source Code: github.com/gitdagray/nodejs_w...
    How to Build a Web Server with Node JS | Node.js Tutorials for Beginners
    (00:00) Intro
    (00:05) Welcome
    (00:14) Overview
    (00:28) Project Setup
    (03:01) Import modules / dependencies
    (04:33) A minimal server
    (08:02) Two inefficient approaches
    (11:06) Setting the Content-Type
    (13:11) Setting the file path
    (15:59) Make the html extension optional
    (17:22) Does the requested file exist?
    (18:18) Routing redirects
    (22:48) Basic serveFile function
    (25:17) Calling the serveFile function
    (28:20) Serving JSON with the serveFile function
    (30:25) Serving images with the serveFile function
    (31:59) Sending a 404 status code
    (32:56) Logging requests and errors
    (35:51) Up and running with the web server
    📚 References:
    Node.js Official site: nodejs.org
    NPM Official site: www.npmjs.com/
    ✅ Follow Me:
    Twitter: / yesdavidgray
    LinkedIn: / davidagray
    Blog: yesdavidgray.com
    Reddit: / daveoneleven
    Was this tutorial about how to build a web server with Node.js helpful? If so, please share. Let me know your thoughts in the comments.
    #node #nodejs #server
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 94

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  2 ปีที่แล้ว +13

    In this tutorial, we will learn how to build a web server with Node.js without using a framework like Express JS. Using only Node.js will help introduce some foundational knowledge about Node that will continue to help us as we work with Node.js and Express JS in the future. If you are just starting out with Node.js, I suggest starting at the beginning of the Node.js for Beginners playlist here: th-cam.com/play/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw.html

  • @AdityaDey424
    @AdityaDey424 ปีที่แล้ว +8

    I don't know why I got to know about this channel a few days ago. You are a gem & all playlists are well structured. Thanks a lot sir 🙏

  • @jamesmassa1999
    @jamesmassa1999 2 ปีที่แล้ว +12

    Dave this is an excellent series!! Watched each video as it comes out! Please keep producing these excellent videos they are so beneficial!!!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +5

      Thanks James! After this tutorial, the series adds in Express and eventually MongoDB to round out the MERN stack backend REST API construction. I appreciate your note! 💯

  • @elvispontes4165
    @elvispontes4165 10 หลายเดือนก่อน +2

    Excelent tutorial Dave!!! Thanks a lot for sharing!!!

  • @rashedqazizada5125
    @rashedqazizada5125 ปีที่แล้ว +3

    I really enjoy the energy and enthusiasm in your videos. I just wanted to say thank you for creating this and other videos. I appreciate your hard work and dedication.

  • @moiraleon8744
    @moiraleon8744 2 ปีที่แล้ว +1

    Love all of these videos. Thank you so much!

  • @fares.abuali
    @fares.abuali 2 ปีที่แล้ว +4

    Thanks Mr. Dave,
    I have learnt a lot from you and recommended your videos to my colleagues.

  • @mostafahusany1747
    @mostafahusany1747 11 หลายเดือนก่อน

    I watched this video several times and I just want to keep press like 1000 time and it's not enough for your efforts

  • @TravinskiyVladislav
    @TravinskiyVladislav ปีที่แล้ว

    Thank you, Dave

  • @vuejs1
    @vuejs1 10 หลายเดือนก่อน

    This is pure gold😊

  • @mateusbessadev
    @mateusbessadev 7 หลายเดือนก่อน

    Just Amazing!!

  • @vikrambabariya5166
    @vikrambabariya5166 ปีที่แล้ว +2

    learned so many things from this video 👍👍 thank you for this amazing series.

  • @user-rl1os8lq4o
    @user-rl1os8lq4o 2 ปีที่แล้ว +1

    Dave. You're amazing!! love your videos!!

  • @Salma-qy3qb
    @Salma-qy3qb 18 วันที่ผ่านมา

    You're Awesome, Thank you so much

  • @MrSalFav
    @MrSalFav 2 ปีที่แล้ว

    Wow!! speachless, This is really good. THANK YOU !

  • @leomacdon7858
    @leomacdon7858 ปีที่แล้ว +1

    I've made a wise decision to spend time on this TH-cam channel. Thank you Dave.

  • @yogeshkhatri4555
    @yogeshkhatri4555 ปีที่แล้ว +1

    This is it. I want this level of clarity even if it is among the most basic ones. HatsOff bro. keep going

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  ปีที่แล้ว +1

      Glad to hear that!

    • @yogeshkhatri4555
      @yogeshkhatri4555 ปีที่แล้ว

      @@DaveGrayTeachesCode Can u tell me like how can we optimize a server like we can using bundling in a client that can remove all reusable files.

  • @kakhabervk9405
    @kakhabervk9405 ปีที่แล้ว

    This is best example of node server I did watched!

  • @thesrinathkamath
    @thesrinathkamath 2 ปีที่แล้ว +1

    Second!
    Great one bro!

  • @bardhan.abhirup
    @bardhan.abhirup ปีที่แล้ว

    Such a great tutorial!

  • @saxa1165
    @saxa1165 ปีที่แล้ว

    Thanks! It helps a lot!!

  • @BrainNotUsed
    @BrainNotUsed 2 ปีที่แล้ว +1

    Superb content as always!

  • @alikisang827
    @alikisang827 ปีที่แล้ว

    Dave is an excellent teacher☺️☺️👍

  • @ahmad-murery
    @ahmad-murery 2 ปีที่แล้ว +3

    I like to know how stuffs work and maybe this is why I like building things from scratch,
    I might use Map instead of the extension switch but I know this is just an intro to node server so refactoring is not beneficial,
    Great work as always my friend 🚀👍

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +2

      A great observation Ahmad! I also like building from scratch - such a great way to learn. Thanks for the note. We will be moving on to Express JS next!

  • @a7medabdulshafi887
    @a7medabdulshafi887 ปีที่แล้ว

    Awesome

  • @iconicscene3768
    @iconicscene3768 7 หลายเดือนก่อน

    Hi Mr.Dave,
    I have a question concerning the parse and the stringify part of this lecture. Would it have been better if we serve the json data without parse in it and stringify it again as long the readFile() for .json extension do that already and the receiver could parse and consume the data correctly?
    by the way i can't thank you enough for all what you've done seen much content but you've a unique philosophy ( Tought you the right way of doing it ). Am sure your hard work will be remembered forever ☺.

  • @sonamohialdin3376
    @sonamohialdin3376 2 ปีที่แล้ว

    Amazing tutorial

  • @chetanjain4616
    @chetanjain4616 2 ปีที่แล้ว +2

    Hey man, love your videos as a suggestion can you make a video or explain what is Cumulative Layout Shift (CLS) ?

  • @tech5956
    @tech5956 ปีที่แล้ว

    Great video 😍

  • @TheNamesJT
    @TheNamesJT 2 ปีที่แล้ว +2

    13:27 chain ternary hell

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +2

      I know I differ from some, but I like chained ternaries much better than large if statements. Feel free to use your preference. 😃

  • @wedangstudio6354
    @wedangstudio6354 ปีที่แล้ว

    thank you

  • @abdulrahmanagboolaosho3582
    @abdulrahmanagboolaosho3582 ปีที่แล้ว +2

    why do you love indentation so much, I have even picked up the habit from you

  • @anjaneyulub2529
    @anjaneyulub2529 10 หลายเดือนก่อน

    Hi Dave can u mention which vs code theme and icons u use?

  • @codinggyan1009
    @codinggyan1009 ปีที่แล้ว

    very nice this video

  • @TinyMaths
    @TinyMaths ปีที่แล้ว +1

    Not sure if anybody else had the same trouble as me; currently looking for an answer. Images won't render to the page. I've checked the switch statement for extension types using (for example with png images):
    switch(extension){
    case '.png':
    contentType = 'image/png'
    break;
    }
    but I logged extention (from inside the switch statement); which confirmed that the extension is png, and contentType from the serveFile function; but instead of contentType being given as 'image/png', it is being given as 'text/html' as if the variable isn't working properly. It works for .txt, .html, and .json extensions, but it looks like CSS and image extensions return contentType as 'text/html'. I even tried using 'PNG File' as the file type (as that's how they are named in Windows); but contentType won't change from 'text/html'.
    EDIT: came back to say problem solved. For some reason the file types were being missed so I created an if condition of 'if(extention && contentType !== 'text/html'){ filePath = path.join(__dirname, req.url). That worked for the CSS, but the images were not showing and, as per the condition for when files don't exist, the text/html was being used as contentType. I also noticed for my image named 'rubber duck.png', the req.url was being logged as 'rubber%20duck.png' and I wondered if that was the reason it was not recognized and maybe the path (or http) module putting '%20' in place of a space between image name words was causing the issue. I replaced the space with a hyphen and 'voila', the images showed.

  • @HOW-bt3et
    @HOW-bt3et 14 วันที่ผ่านมา

    This is a perfect Tutorial , Thanks man. And I have a question I hope I don't make you feel bad just asking How old are you ?

  • @alirezahekmati7632
    @alirezahekmati7632 2 ปีที่แล้ว

    🚀

  • @haiderjaafer8164
    @haiderjaafer8164 2 ปีที่แล้ว +1

    Great work keep going forward… can I use this in the restful api

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +2

      Yes, keep going in the series and we add Express to build a RESTful API.

  • @thebocksters2756
    @thebocksters2756 3 หลายเดือนก่อน

    1:39 why we extends EventEmitter and not use it independently

  • @elinordeniz
    @elinordeniz ปีที่แล้ว

  • @haiderjaafer8164
    @haiderjaafer8164 2 ปีที่แล้ว +1

    absolutely informative lesson but there is some lines of code did not understand ... keep going

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +2

      If there are lines that are confusing, I suggest console logging values to see what each one is doing. Doesn't hurt to play around with it for understanding. 💯

    • @haiderjaafer8164
      @haiderjaafer8164 2 ปีที่แล้ว +1

      @@DaveGrayTeachesCode
      I already trying to do console log .. thanks 🙏

  • @LemanGahramanova-kh8fb
    @LemanGahramanova-kh8fb 2 หลายเดือนก่อน

    It's so good tutorial for me, but I have a question, for example in browser when i write localhost:3500/subdir -> it doesn't work but localhost:3500/subdir/works I found some solutions, but they don't work, how I can solve it? Is anyone knows?

  • @thesrinathkamath
    @thesrinathkamath 2 ปีที่แล้ว +1

    Can you deploy this on shared hosting (Not VPS, not cloud) via terminal?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      It just needs a host that supports Node.js. For example, Heroku has their own CLI which would let you deploy from the terminal. You could also set up continuous deployment from GitHub to Heroku or another host if the host supports it.

    • @thesrinathkamath
      @thesrinathkamath 2 ปีที่แล้ว +1

      @@DaveGrayTeachesCode No need of node support bro. If you can share me your email, I can send u the code and take a look and probably make a video of it.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      I must be confused. I thought you were asking about this Node.js tutorial project as shown in the video. The host service definitely needs to have support for Node.js to host this project.

  • @francescotagliavento8081
    @francescotagliavento8081 2 ปีที่แล้ว +1

    which extension do you have for icons?

  • @immortalaigs4203
    @immortalaigs4203 2 ปีที่แล้ว +1

    Where can i practise this kind properties Dave?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      I always recommend freeCodeCamp.org to my students. It offers many exercises to help you practice. There are both Javascript and Node JS sections there.

    • @immortalaigs4203
      @immortalaigs4203 2 ปีที่แล้ว +1

      @@DaveGrayTeachesCode Thank youu so much Mr Dave !;

  • @dafiraone
    @dafiraone ปีที่แล้ว

    How to show the length of the block of code inside curly braces? like on 10:33

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  ปีที่แล้ว +1

      VS Code settings: Bracket Pair Colorization: Enabled

  • @wildcat718
    @wildcat718 ปีที่แล้ว

    why on 27:36, it is just text, not JSON? what's difference between 27:36 and 30:26?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  ปีที่แล้ว

      At 27:36, we have not configured the server to handle sending a JSON response yet so it is defaulting to the text response that is configured. It looks like JSON, but it is a text response. By the time we reach 30:26, we have configured our server to send a response type of application/json.

    • @brandonjoaocastillo7490
      @brandonjoaocastillo7490 ปีที่แล้ว

      if you try in Firefox it would be more obvious, the Browser knows when the data is plain text or json, you should try in Firefox, it makes things more clear

  • @shanglee643
    @shanglee643 2 ปีที่แล้ว +1

    first.

  • @mohammad._.abohasan
    @mohammad._.abohasan ปีที่แล้ว

    Thank you for this wonderful video

  • @nazmulalam4874
    @nazmulalam4874 ปีที่แล้ว

    I am noob this is hard for me

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  ปีที่แล้ว

      Don't give up, but as a noob, have you learned Javascript first? You should learn Javascript before starting Node.js.

  • @shineLouisShine
    @shineLouisShine ปีที่แล้ว

    Thank you very much.
    37:20 -
    Why in my code, despite writing exactly the same as you did
    (`${err.name}: ${err.massage})
    And despite of deleting the exact same letter as you did for the purpose of occuring this error,
    Yet, the error in the my errLog.txt file appears as: " TypeError: undefined. "
    Instead of displaying the error's name and message..?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  ปีที่แล้ว

      There must be a difference even though you suggest it is exactly like mine. For example, in your comment you said: err.massage ...but it should be err.message

    • @shineLouisShine
      @shineLouisShine ปีที่แล้ว

      @@DaveGrayTeachesCode You are sharp :)) Thank you!
      An automatic typing error could be a major assistance.
      Sometimes I'm surprised of how advanced those softwares are,
      While being kind of primitive regarding typing errors handling.

  • @1conscience0dimension
    @1conscience0dimension 2 หลายเดือนก่อน

    00:17:40 no need for the last line (if (!extension && req.url.slice(-1) !== '/') filePath += '.html';)
    you just needed to add one more level of craziness to the ternary
    contentType === 'text/html'
    ? extension === html ? path.join(__dirname, 'views', req.url) : path.join(__dirname, 'views', req.url)+".html"
    : path.join(__dirname, req.url);
    😂