This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Setting Up Express Routes" Lesson
>> Scott Moss: Cool, so now that you were able to get your feet wet creating some basic routes with Express, I'm just gonna walk through a slightly more complicated example. So if we hop into server, we're gonna go in here and say, let's just do a GET request to /data this time instead of just doing slash.
[00:00:18] And it will go ahead and make our controller function here. We'll pass in our two arguments, the first being the request, second being the response. And then what we wanna do is just serve up some data. We'll just say response.send, and we'll just put a message here that says,
>> Scott Moss: Hello, so now that we have some data sending there, and then we'll do another one for post. So we'll do a post request here to the same route. And remember, I'm sorry, to data, and although that both of these have the same route to /data, they're two different verbs.
[00:00:55] So we're doing a get request to /data. This one's on a post request to /data. So it's a different match here. So we won't have any conflicts here. So we'll do a request, we'll do a response. And in this case, let's just know that the body that's being sent to this post request is gonna be attached to a property called body on request object.
[00:01:17] And we'll talk more about that in the future lessons, but that's gonna be free for us. So what I'm gonna do is I'm just gonna go ahead and just send that back. So I'm just gonna say res.send, and then I'll say req.body. And that will go ahead and send back the body that was sent to us.
[00:01:35] And the next thing we have to do is start the server. So we'll go down to the start function that we have. I will say app.listen, and it'll listen on port 3000. And we'll run our callback function here. And just to put a log in here, just so we know our server's running, we'll say server on port 3000.
[00:01:55] So now we go ahead and start the server and we run a yarn dev. Or if you're doing NPM, npm run dev.
>> Scott Moss: And if you scroll down, you can see servers on port 3000. So let's head over to Insomnia. And we need to change this to a get request to /data, and we'll go ahead and run this.
[00:02:15] And you can see I get back the message hello, which is what our REST put in the server here. So message hello. And then I would do a post request to data and we should just get echoed back what we post. So post request here, make sure we change that to /data, and then we'll just send some data here.
[00:02:35] So let's just put an email.
>> Scott Moss: firstname.lastname@example.org, so we'll send that email there, and you could see we get that back echoed.