Chatty Chat - Part 4: Join/GET a Chatroom with a Unique ID

Node.js Express.js JavaScript
Transcript

English (Auto-generated)

Hi everyone in the last part we created a unique random number for each chat room. Now what we want to do is to request a chat room with a certain I. D. So this means using a get request on a route that has a specific room I. D. So in the code editor and I'm still working in index dot Js. Um Let's just go ahead and structure this. So app dot get and the route will be room ID. And then in response uh we need to have a callback function with the request and response objects as the arguments. Okay so out here the forward slash colon room I. D. Is written like this instead of simply like so because it's a way of declaring a VR a variable in the U. R. L. And the value in the U R. L can be passed in any normal request. So we access the room I. D. Via U R. L. Parameters with the help of the request object and the request to this path contains the room I. D. Anyways, so now let's go ahead and check if a valid room I. D. Is present. If so then we can handle the response accordingly else will show an error message. So first off let's go ahead and declare a local variables. So like valid room I. D. Is equal to U. I. D. Got validate and then passed in the request programs dot room I. D. Okay so the validate method of U. U I. D. Will validate if the generated unique I. D. Is generated by U I. U U I. D. Package or not. Okay so out here we're checking if this is a valid unique identify, rare or not. And if we pass in any random string in here we'll just get false. So to demonstrate this. What we can also do is well let's just carry on and then we'll uh basically we can also do console dot log. Right? So let's just see this. So valid room I. D. Okay. And control as and basically now I'm going to go to the browser and then remember that we have to out here do forward slash create room which gives us the link for a particular room because we haven't worked on the front end as yet. So control C. So this is my uniquely generated random number. And out here I am going to copy paste that. And now in this is going to hang because I haven't handled the request as yet the response. Right? But if I go to my console dot log you will see that request programs dot room I. D. Refers to the valid room I. D. Variable. Right? So if the valid room I. D. Variable does not reference a valid random uniquely generated I. D. Then we want to send a 400 error. Jason response. So let's work on that next And out here. What I'm going to do is I'm going to say well if so let me go ahead and structure my if else block an end of if so if not a valid room I. D. Right then I want to return a response with a status of 400 which means error and and then Jason so doing the same as we did earlier and this is going to have a status of fail and out here I am going to then um oops syntax mistake, status of feel and a message which is going to see invalid room I. D. Okay remember that the response dot Jason function sends a Jason response and here we have two key value pairs status lets the user know that the request was unsuccessful. And then we also show a message which says invalid room I. D. Now if the variable valid room I. D. Variable does indeed reference a valid room I. D. Then we will declare a new local variable. So let file pack is equal to using the path dot join method. So directly name and remember that this um are two underscores it's not just one underscore and then passing in the back two this room dot html file. So the room dot html file references this html file that you should have in your folder. And coming back here. So the path dot join method joins the specified path segments into one path and will underscore their name is an environment variable that tells you the absolute path of the directory containing the currently executing file. And um if you are confused about this you can just console dot log um your name. Right and out here to this fat then right we are joining the static room dot html file. Inside inside the public folder because that is the file we want should be sent back as a response to the user and we will send this whole path back as a response using the sand file methods. So return response got sent file and I'm going to be sending a file path. Right? So the uh control s to save. Right? So this is the file that we want should be sent back to the user as a response. And we're sending this whole path back as a response using send file method which is going to send the room dot html file. That is accessible by this file path variable which basically gives the whole path to that file. So let's go ahead and actually test this out and out here we could have an else statement but anyways let's just check this out first. So if valid. Room I. D. Well okay this is what we don't want to do basically this was inside the if I'm going to move it outside of the f. There you go. So out here we are dealing with if um valid room I. D. Right and out here we're dealing with the use case if not valid room I. D. Okay so let's go ahead. Control us to save and I'm just gonna now test it out in the browser and go back. Okay, we're on the create room bath and out here you see every time I refresh I basically have a new idea. Random I. D. Generated. And now what I'm going to do is I'm going to go ahead and control see this and then I'm going to place it out here and what we're expecting is we're expecting to see room dot html. Right so enter. Okay. Their name is not defined so we have a small error and it seems like it might be a very small thing. Let's just check it out. Um your name. Directory, their name is not defined. So this is console dot log. Oh there you go. So this console dot log. It's a syntax area. So I had a double N. So control s and now let's go ahead and control are and there you go. So now I have my room dot html working as expected. And if I change one of the characters out here, let's for example, let's add the character A You're going to see that we have a status of fail and message of invalid room I. D. So it's not going to work if it's not a uniquely generated random I. D. Using the U. U. I. D. Package that we used. So now in the next part we're going to be working on passing this link to the front end. Right because we can't expect users to be accessing code files and working in the terminals to use the application. And another thing that I wanted to show you is that here you can see the journey, environment variables, value that we were talking about. So that's all for this cast. And in the next cast we'll work on passing the room link to this front end so that when someone clicks on here, they should see that uniquely generated random I. D.
125 Views 0 Likes 0 Comments

In the last Cast we created a unique random number for a chatroom. Now we will be requesting/GETting the chatroom webpage associated with a certain ID

Comment
Leave a comment (supports markdown format)