Chatty Chat - Part 2: Setting Up the Server

Node.js Express.js JavaScript
Transcript

English (Auto-generated)

So let's get a basic server up and running and I am working in index dot gs. So this file which is in the main directory. So first off we'll start off with Let Express is equal to require. So I'm going to require the express package. And while I'm at that let's go ahead and also basically require um everything else that we installed. So require uh new I. T. And then we also wanted to uh let's require a bath module so require at and then I'm going to say well let pork is equal to 3000. And um let app is equal to um an instance of express. Okay so we've required Express and we've instant enshi ated a new instance of an express app and we're also using the path and U. I. D. Variables later on. So why not just require them right now? And we're going to be like a server is going to be listening on Port three 1000. And for now all that we want our app to do is to listen on port 3000. And when uh it gets a request on the main homepage we're just going to send a string uh you know such as hello in response. So let's go ahead and do this app dot get And when uh the app gets a get request on the home page then we are going to use a callback function which takes the request and response objects as arguments and in response. We just want to send the string Hello. Right and that's all that we're doing for now and then we want to at last listen out on the report, which is 3000. So once again, let's just control us save this now. We're not our server is not running. So let's go ahead and run our Silver now to run the server. You can do node and then index dot Js. Right. And now I am going to go to local host on port 3000 and try this out. There you go. So now you can see the string. Hello now a short tip. Is that every time that we make a change to our project files, we don't necessarily want to keep starting a silver. It's it would be nice if the silver automatically restarted every time that we made a change and saved our project folder so to do that we can control C and basically in the terminal I'm going to npm install a Nordmann. So that's a package. The name of the package. So I'm installing a node one in my project, which is basically oops. Directory. Okay. Which is basically going to just restart the server every time that we have a change. And the first time that you use node one, you do have to specify the file name, that no one needs to restart automatically. So for us that is index dot Js and there you go. So once again, if I go back here and refresh, it's working and for example, if I made a small change such as Hello, World control. S I don't need to do anything in the terminal, I just refreshed and you know my latest change is reflected. Okay. Now what we need to do is we need to serve our homepage. Which out here is this index dot html file under the public directory. Right, so let's go ahead and work on that next. So in index dot gs where we have a silver code, I am going to say well app dot use and I'm going to use the public directory and then express a dot ecstatic. And once again public. So this is the file, the static files which I want to serve our under the public directory. And now, instead of ascending Hello world out here, let's actually um you know, modify this request. So we're asking our app to use a middleware for serving static files to our express app. And now we are going to basically which we're going to be joining the homepage file path with the main directory path and uh send use the scent file method to send the homepage which is index dot Gs. Now the sand file method is available on the response object and it can be used to stand static files to the client. So let's go ahead and do that. So I'm going to comment this out and out here, I'm going to say well let a file path is equal to path dot join. So remember we already required the path module out here. Online number three. So path dot join. And then the directory name and the directory name is going to be mm hmm public. And then in index dot html in the public directory. And then in response, I am basically going to return response object and using the send a file method to send this uh, file path. Okay, so control S and out here. If you notice in my criminal, I don't need to do anything. It just basically restarts at this survey whenever I make a change and safe. So once again, let's go back here and then control. S and there you go. So we've now served our index dot html file which is under the public directory. And next, what we need to do is we need to work on this create button here which when we click on will give us a link. And basically people are then going to, you know, either copy, paste the link in the browser or just click on a joint button which will dynamically create and join and be able to join a chat room
186 Views 0 Likes 0 Comments

Let's instantiate an express app and get out server up and running.

Comment
Leave a comment (supports markdown format)