Digging Into Node.js

Express JS Routing

Kyle Simpson

Kyle Simpson

You Don't Know JS
Digging Into Node.js

Check out a free preview of the full Digging Into Node.js course

The "Express JS Routing" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle modifies the server to use Express instead of manual routing, instantiates the Express app, and configures the routes Express will handle by declaratively stating the route inside of the app.get method.

Preview
Close

Transcript from the "Express JS Routing" Lesson

[00:00:00]
>> Kyle Simpson: We've built our own web server and static file server and API endpoint. But the truth is, you're almost certainly gonna be working with one of those more well known frameworks. And I thought it would be a good idea to at least get you a little bit of exposure to probably the most well known of those, which is Express.js.

[00:00:17]
So in exercise six, we're gonna just switch over everything that we were doing on the server side from doing a kind of manually to using Express.js. I've already got express installed for you, which is actually a good thing because if you try to do npn install express, you'll see the download is something like 300 packages.

[00:00:34]
It's kind of got a lot of dependencies. But anyway, we've got express already installed for you. And we are gonna start wiring it up to using express. The way express works is you call the express function and you can pass some config but we don't need to. And you get back a thing which is almost always called app.

[00:01:01]
So that represents your express app, this thing called app. And you may wonder what is app, is it some kind of object, is it instantiation of a class? To understand what app is, let's see how we use it. We actually use app, by passing it to our HTTP server instance.

[00:01:23]
If you remember from the previous exercise, what did we pass to create server, we passed the function handle request that we defined. So in other words, app is an automatically generated handle request function. That receives req request and response streams and handles them. And in fact, inside of that app function, if you were open up its guts and look at it, is basically just going to be a fancy for loop with some if statements in it.

[00:01:52]
Okay, so we are not going to define and handle requests anymore because that's being done for us by Express. But we need we do need to configure Express so that it knows what routes we wanna use. So, we are going to do that by defining routes for Express.

[00:02:14]
I'm going to create a function called define routes.
>> Kyle Simpson: We'll actually move this stuff probably into there.
>> Kyle Simpson: And then we'll just call that defineRoutes function. So, that's kind of like the placeholder for our handle request and it's the placeholder for our node's static alias, cuz we're gonna be defining all these routes.

[00:02:50]
And just for hint's sake, so that it's easier for copy and pasting, these are the alias routes that we set up previously when we were using node's static alias. We're going to need to do the equivalent of those with express. So how do we do that? Well first off we have to tell app, which is again it's our function object.

[00:03:12]
But we've got to tell it what routes to set up. And we do that by defining, in express.js terms, we do that by defining something called a middleware. And that's a fancy way of basically just saying a function that gets called if the incoming request matches some particular criteria.

[00:03:31]
So we're gonna set up a couple of different middlewares here that are the equivalent of these routes that we were handling.
>> Kyle Simpson: So first let's handle our API endpoint. You recall that we had, we wanted to handle our API endpoint. Let's call app.get, that's how we tell it that we wanna handle inbound get requests.

[00:04:02]
And we're gonna give it a URL, in this case, it's gonna be a string, we could have also done a regular expression. Remember we called it get-records. And if an incoming request matches that we're going to receive req and res
>> Kyle Simpson: And we want for this route to do exactly the same thing as it was doing in ex5, which was we were getting the records.

[00:04:30]
So I'm gonna make this an async function just to be safe. We're gonna get the records from, get all records, and then we're going to res.writeHead.
>> Kyle Simpson: That's basically the exact same code that we did in EX5. And there are a whole wide range of plugins that express provides.

[00:05:34]
And I know that there are a couple of out a couple of them out there, which specifically give you a little bit of syntactic sugar for doing, say, a JSON response like this. You don't have to come up with your own content type header or whatever. But what you're gonna find with express is that what ships out of the box, even though there's a ton of dependencies, what ships out of the box doesn't even really do some of these basic things.

[00:05:59]
You end up having to go searching around the ecosystem for somebody having written a plugin to do XYZ. So, you end up using Express plus a variety of Express plugins for doing these things. And just to save time in terms of our learning, we're just gonna end up repeating things.

[00:06:15]
But the point I want you to get here is that instead of writing an if statement somewhere, we declaratively state the route by calling app.get. And Express will internally match to make sure that any get methods with that URL will call this function.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now