Node Events Tutorial | Node.js Tutorials for Beginners

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ค. 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    In this Node events tutorial, you will learn about the events common core module and how to create an Event Emitter. The Node.js Tutorials for Beginners series takes you from zero knowledge of Node JS to building a web server.
    🚩 Subscribe ➜ bit.ly/3nGHmNn
    🚀 This lesson is part of a Node.js for Beginners tutorial series playlist:
    • Node.js Tutorials for ...
    Node Events Tutorial | Node.js Tutorials for Beginners
    (00:00) Intro
    (00:05) Welcome
    (00:14) Overview
    (00:25) Setup
    (01:21) Building a custom log module
    (06:33) Events module and EventEmitter
    (09:40) Finishing the logEvents module
    (14:25) Wrap up
    📚 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 events and the Event Emitter in Node.js helpful? If so, please share. Let me know your thoughts in the comments.
    #node #nodejs #events
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Learning how to create an event logger is very useful for a backend application running on Node JS. In this tutorial, we'll cover the Event Emitter in Node.js and create an event logger. If you are just starting to learn Node.js, I recommend starting at the beginning of this Node.js for Beginners playlist: th-cam.com/play/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw.html

  • @skillkrio
    @skillkrio 10 หลายเดือนก่อน +5

    This Goat will unquestionably make me as a full stack development. Concise video packed with abundance of information. Thanks alot Dave

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

    Very good video. Also, we can use the eventEmitter without a class, just initialize the ee
    const {EventEmiter} = require('events')
    const myEvent = new EventEmitter();

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

    One cool feature about events is that you emit an event once but you can have multiple listeners on it,
    for example we can have two listeners listening on the "log" event, one of which is responsible for writing the data on the disk and the other is responsible for displaying it in the console,
    maybe later on we need a third one for sending emails (ex: to developer) when a specific data is logged (ex: uncaught errors/exceptions),
    the good part here is all of these logging implementations are independent of each others, so we can drop or modify any of them as needed without having to touch the code where the event is emitted.
    Thanks Dave,

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

      Spot on my friend! Great note 💯🙏

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

      @@DaveGrayTeachesCode it's rather a humble note to a great content my friend 👍👌🚀

  • @BlueHat1
    @BlueHat1 หลายเดือนก่อน +1

    What an incredible video! Thank you so much for explaining everything so well :)

  • @erichgq1
    @erichgq1 8 หลายเดือนก่อน

    I know that you posted this video a while back but I am trying to lean more about node and web servers etc. and I will say this has been a pleasure to watch and try. I am 70 but I just enjoy dabbling in code. Your video is easy to follow and easy to understand. Thank you for taking the time to show us how to do things and make them work. Your folder missing is a perfect way to trap errors. Cheers

  • @codewithkin
    @codewithkin 9 หลายเดือนก่อน

    Thanks a lot for this Dave! Your courses are amazing, you are so underrated. I will share with everyone I know, and congratulations!

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 9 หลายเดือนก่อน +1

    Thank you so much! Very well explained! 💛

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

    Thank you so much for this series. It really helped me a lot.

  • @caaltz
    @caaltz 6 หลายเดือนก่อน

    I was on vacation and had to remember some stuff on node, thank God I found you

  • @imakshith
    @imakshith 5 หลายเดือนก่อน

    Thanks a lot for this Dave! for sharing such an amazing content!

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

    nice refresher for us

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

    Excellent didactic. Clarity and objectivity. Thank you one more time. It has helped me in my work.

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

      You're welcome, and thank you for the kind words! 🙏

  • @tat-lyubch
    @tat-lyubch 7 หลายเดือนก่อน

    Brilliant and simple as always. For people to take a first impression about subjects to involve. So we should be a good students. 😊

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

    Awesome series 👌😀

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

    Dave man...you are my hero

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

    Awesome tutorial for an absolute beginner to nodejs like me. Thx a lot!!!!!

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

    Thank you, this is really good tutorial.

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

    Love node, Awesome. also expecting for express tutorials

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

      Thank you! 🙏 Yes, I'll be covering how to create Node.js only web server next and then the series will introduce Express to follow. 🚀

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

    Really amazing

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

    Awesome tutorial thank you

  • @leechiwaimichael7131
    @leechiwaimichael7131 9 หลายเดือนก่อน

    watching you tutorial video pretty cool. On the other hand, wish to learn how to developing a payment gateway on the E-Commerce when I getting an online order to forward UPS and Federal Express

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

    Its me. Again. and again thank you

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

    Hi Dave, Whats the purpose of UUID in the error log? Isn't the datetime unique enough to serve as an identifier?. I am almost finishing this series and have learned so much from your tutorials. Thanks you so much!

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

      Possibly. It all just depends on how you want to track the data. I'm giving one example. Overall, I'm showing concepts and you can apply them differently.

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

    🚀

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

    hi dave are you mean common core modules is existed or embbeded in node js previousely

  • @Web-Himansu
    @Web-Himansu ปีที่แล้ว

    Sir best resource to learn nodejs and mongodb

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

      My free course covers Node.js, Express.js and MongoDB: th-cam.com/video/f2EqECiTBL8/w-d-xo.html

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

    the ocnstant variable `EventEmitter` why is this not camel cased???

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

      It is a class. Here is the docs link: nodejs.org/dist/latest-v16.x/docs/api/events.html#class-eventemitter

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

    it's actually console.error(), I tried console.err() in the past too lol.

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

      Yes, 😂 I hope you saw my overlayed note in the video that offered the correction.

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

    what is the real use of EventEmitter ?

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

      It emits (aka releases or sends) events. Reference: nodejs.org/dist/latest-v18.x/docs/api/events.html#class-eventemitter

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

      @@DaveGrayTeachesCode thank you for reply

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

    Hi Dave,
    thanks for the video but I have to say that this was not helpful. You did use events but it wasnt clear why. Why using events when you can just call the exported function instead of calling emit? 95% of the video was puting together the log function instead of explaining events at all. I would have liked to get more in depth knownledge how events work and when to use them. Im my opinion you are totally missusing them.

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

      While I've helped many, I realize my teaching style isn't for everyone and I'm far from perfect. Keep going in this series, and you will see how I use an event in a practical way as we build a REST API.

  • @fardinahsan4319
    @fardinahsan4319 11 หลายเดือนก่อน +1

    Why is the output like this?
    31.07.2023 01:03:56 function v4(options, buf, offset) {
    if (_native.default.randomUUID && !buf && !options) {
    return _native.default.randomUUID();
    }
    options = options || {};
    const rnds = options.random || (options.rng || _rng.default)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
    rnds[6] = rnds[6] & 0x0f | 0x40;
    rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
    if (buf) {
    offset = offset || 0;
    for (let i = 0; i < 16; ++i) {
    buf[offset + i] = rnds[i];
    }
    return buf;
    }
    return (0, _stringify.unsafeStringify)(rnds);
    } Log Event Emitted!

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

      index.js =>
      const logEvents = require('./logEvents')
      const EventEmitter = require('events')
      class MyEmitter extends EventEmitter{ };
      const myEmitter = new MyEmitter();
      myEmitter.on('log',(msg)=>logEvents(msg))
      // setTimeout(()=>{
      //Emit Event
      myEmitter.emit('log','Log Event Emitted!')
      // },2000)

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

      logEvents.js =>
      const {format} = require('date-fns')
      const {v4:uuid} = require('uuid')
      const fs = require('fs')
      const fsPromises = require('fs').promises
      const path = require('path')
      const logEvents = async (message)=>{
      const dateTime = `${format(new Date(), 'dd.MM.yyyy\tHH:mm:ss')}`
      const logItem = `${dateTime}\t${uuid}\t${message}
      `
      console.log(logItem)
      try{
      if(!fs.existsSync(path.join(__dirname,'logs'))){
      await fsPromises.mkdir(path.join(__dirname,'logs'))
      }
      //testing
      await fsPromises.appendFile(path.join(__dirname,'logs','EventLogs.txt'),logItem)
      }
      catch(err){
      console.log(err)
      }
      }
      module.exports = logEvents