Chatty Chat - Part 3: Create a Random Unique ID for Each Room

Node.js JavaScript Express.js
Transcript

English (Auto-generated)

hi everyone in this part, we're still going to be working in the index dot Js file. And what we're going to do now is we'll create a random unique I. D. For each room because every time that we click on this create button here we want to generate a chat room with a unique ID for other people to join. So let's go to index dot Js in the code editor. And right after the homeopath we're going to be working on the create room path. So app dot get and this is gonna be a create room so forward slash create room. And then whenever someone requests the create room U. R. L. Or the path. Then in response we want to do something. So we are now going to be working on the response park. So what do we want to do? Well first of all we want to generate that unique random identify rare And for that we're going to be using this U. U. I. D. Module And out here I'm going to declare a local variable called room I. D. Is equal to. And then we're using U. U. I. D. Got version four. So this is going to generate a unique random room I. D. Okay. And then what we can also do is just to make sure we can console dot log this room I. D. So let's go ahead and check actually check this out first. And I am going to control S to save and in my terminal I already have um Nordmann running so out here I am actually just going to go to my hoops browser tab And out here create room and nothing's gonna happen. It's just gonna hang. But what I do want to show you is that this unique random I. D. Is generated. So let's go back to the code editor and now we are going to return as a response status. So let me just type this out and then I will briefly explain exactly what we are doing here and status success and then link is going to be room I. D. Okay so first of all we're generating a unique room I. D. Which is a version for random U. U. I. D. And assigning it to the local variable room I. D. Out here. Now once that were that is done we're going to end our response with a 200 success status which means all went well. And the response. function is going to send a Jason response. And out here we have two key value pairs and status lets the user know um You know that the request was successful and then we have a key called link whose value is the unique random room I. D. Which was generated. Right? And what we can do out here is we can actually put all of this in a try block. Right? So we have try and catch blocks. So I'm just gonna put this in at the cry block. And then we are also going to have a catch block And the catch block is simply just going to catch for us. Any errors. Right? So we're going to handle the error inside of the catch block. And inside of here what we're going to do is we're going to send right so we can do return a response uh dot status, right? And the status is going to be a 400 status. So there's some error. And then once again also sending some Jason as a response. And out here uh this has the status key whose value is error. And then we have a message whatever is the error message. Right? So the message property of the error object. And out here end of cry. So just commenting it out. So what we're doing is we're handling the area inside the catch block and here we are sending a status of 400 if something failed. And we also send the error message which actually helps us in debugging in case something goes wrong. So I'm going to go ahead and control s and now go back to the browser. And so out here you can see that I am now generating my uh unique random I. D. And I also see the status which is a success. So there's no error out here which is why we're not seeing any error message. But every time that I refresh the webpage, something that you should notice. Is that a unique room I. D. Is created. So let me go ahead and change this. Refresh it again. So now this room I. D. Ends with three A. C. And once again refresh this one ends with the one a. So that's all for this cast and in the next cast, what we're going to do is that we're actually going to get a chat room going with a certain idea, which means that um you know, we can user can actually uh click on here and the user will be able to see the unique I. D. Which is created.
112 Views 0 Likes 0 Comments

Let's start the process of creating chatrooms with random IDS in this cast.

Comment
Leave a comment (supports markdown format)