One of the best features of express is how easy it is to work with routing. Routing allows you to give users access to different types of data as well as pass information through the params variable of the request object.
- [Voiceover] One of the core features of Express is called routing. Routing allows you to get information for different parts of your website to users. Now we've already seen how we can use app.get to create a route for home, so let's take a look at how you create other routes. Now routes for other URLs are created in the same way as our homepage, with the app.get statement. The interesting thing is that you can pass variables to app.get and receive them into the route with a special variable available through your request called params.
So if we go into our application, I'm gonna clean this up because I still have a lot of my old node js code down here, and what I want to do is create a different route, and I've got a really nice route that is showing me all the speakers, so I'm gonna make a copy of this route right here, and just paste it down here, and I'm going to change the name of it down here to Speakers. And that's how you create a different route, you put in a different piece of text right here, and it allows you to go to another URL of slash speakers.
Now let's go ahead and modify the original root route, which just goes to the homepage, so that it doesn't have all this other stuff. So, our get statement's not gonna have this info variable, and our sent statement will just have a welcome message here and we're not gonna use the info variable, we're just going to type in a paragraph and some text. So let's go ahead and save this, and we'll fire up our application, if it's running you can hit control+C.
Now let me go ahead and clear that out, and just type in node app/app js, it's going to run by default on port 3000. And now we can refresh our browser and see that we have a welcome route that shows this welcome message, and if we modify the URL to have slash speakers at the end, then we can see the rest of our information. So this essentially gives us a couple of different URLs that we can get to. Now what if we wanted to get to just the information for a specific speaker? In order to do that, I need to be able to pass along some information through my route.
So let's go ahead and add another route, it's gonna be similar to the speaker's route. So I'll copy this one and just paste it down here and modify it a little bit so that it receives that information from the URL. So in this case, when we pass along a variable, we add a slash and then a colon, and then any variable we want here. So we can say, speakers, speaker ID, and I don't need this huge info variable, I will create a variable for the individual speaker, and I'm going to set that to our data file, which comes from our adjacent document, and then I'm going to ask for the speaker's element.
And then I'm going to use a variable that comes along through our request, and it's called request, or req, params, and then I can access the speaker ID, I'll put a semicolon here at the end. So, this is called the req because when I created this get method, I passed along this variable and I named it req, sometimes you call it request, but I just chose req, so that variable has a params object, and in that object would come any variables that we pass along through the URL.
So in here, we can just create an H1 and use the ES6-templating variables to pass a speaker title. Then we'll do another headline tag here, and use the speaker name, and then we'll create a paragraph, and I'll copy one of these, and get the summary. So these variables are coming in from our data adjacent file, and each one of these speakers has a title and name, as well as a summary field right here, so that's where these are coming from.
Let's go ahead and save this, and go to our terminal, and hit Control+C so we stop the current application, and then we'll issue the node command again to run the server. And if we refresh, we can get to this route just fine, if we wanna get all the speakers, we can just type in speakers at the end of that, and we'll get all the speakers, and then if we wanna get a specific speaker, we just pass in an ID, so if I say speakers zero, I'm just gonna get the information for the first speaker.
Remember our arrays in Java Script are zero-indexed, so zero means the first one, one is the second one, and two would be the third one. One of the best features of Express is how easy it is to work with routing. Routing allows you to give users access to different types of data, and we can pass along information to the routes through the params variable of the request object.
In this course, Ray Villalobos walks through the process of creating full-featured websites with these technologies. First, find out how to install each package and leverage Node.js features from within Express. Then learn how to build a basic application with templating engines like EJS; create more flexible, modular code with includes and conditional statements; build APIs to manage HTTP requests to add and delete content dynamically; and configure more complex routing. Along the way, Ray implements features like customer feedback forms and real-time live chat, so you can see exactly what Node.js and Express are capable of.
- Handling HTTP requests with Node.js and Express
- Creating and modularizing routes
- Using templates
- Building flexible views
- Creating an API route
- Dealing with POST and DELETE requests
- Creating live chat features with Socket.IO