Loading
Lesson 04
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Defining our First GET Route in Express

Video Transcript

So I'm going to go to the browser, let's go back to the browser, we're gonna go to HTTP colon slash slash localhost add use the colon sorry, 3000, okay, localhost colon 3000. I'm going to try it out to try it out. So we got a message saying cannot get slash. So that's the express server saying that he did not specify any route at this point you can try slash books for example and it's going to say the same thing. So we're trying to get slash books but the route is not defined so that's what express gives us back okay. Great. Now we learn how to round the server using node app.js and to kill the server control C. Let's go back to our file here app.js. Now we are going to learn how to define a route in express okay. So let's just define a simple route with just an arbitrary data so we can get our get started. To define a route we're gonna do the following between the listen and whatever you did we're gonna add the following. We're gonna call on our app and we're gonna call the method called get okay and then we're gonna say slash this is the path to our route this is going to be the route okay and then you give it some callback or function here that's going to have at least like two basic objects that's the request object and the response object okay. So let's write it typically we write it those guys in express with the abbreviations req and res they stand for request and response respectively. So let's use our narrow function here to define the body of what we want to do when we hit the route slash that's going to be just localhost 3000 slash right. So let's send a response to send a response we're gonna use the res object that's the response object and then just say dot send and whatever you want to send let's just send a string okay welcome to express like so okay so when we hit the route the route route we are going to send back a response with the text welcome to express. Let's get back to the terminal now we have to restart the terminal because we made a change to the code so we got to control C to kill it and we're going to do node app.js to run it again okay now let's go to the browser and try again with the slash route see we got a response welcome to express okay so let's get back to the code and review so we're gonna call the app and we're gonna call a method with the HTTP verb for your request in this case it's a get request it could be post it could be put it could be delete right so and then this is the path this case slash the route route you give the definition to what do you want to do when the user hits this route you request what you're gonna do and then we send a response with our yes object always request response in the signature of this function right here and recall on the send to send some string in this case all right so I think we're good with this lesson and I'll see you on the next one
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: