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

The "Custom Routing Middleware" 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 creates custom middleware to handle URL rewriting and explains what Express does when a request arrives. Then, Kyle fields questions about the usage of Express across the industry.

Preview
Close

Transcript from the "Custom Routing Middleware" Lesson

[00:00:00]
>> Kyle Simpson: I have looked and looked and looked around the Express ecosystem for some good plugin for handling friendly URL rewrites, of the sort that we were doing in exercise five. And I only found a couple of them, and when I installed them it caused my Express app to crash.

[00:00:20]
I was never able to get any of them to actually work. So maybe there's one out there that just doesn't have good SEO and isn't Googleable. But whenever I use Express to do URL rewriting of that sort, I end up falling back to exactly the same techniques, which is basically using regular expression matching and replacing.

[00:00:40]
So the way you end up doing that then is just to have a simple app.use middleware.
>> Kyle Simpson: And what you should be aware of with your own middleware is if you're receiving them is that if they're gonna be asynchronous, which in this case it might be, if they're gonna be asynchronous then you want to, there's a third parameter that you have to call as the callback.

[00:01:10]
That's what lets it know, or sometimes its often called next, I guess. You have to execute next to tell Express that it should move on to the next route. If you don't call next it will assume that you completely handled the response, but if you want it to flow to the next thing you need to call next.

[00:01:28]
Here in app.use, we're not gonna handle the response, we're only going to rewrite the URL from the friendly version to the real version and then hand it over to the next route, which is gonna be our static file server. So we do definitely need to call the next function.

[00:01:47]
Before I even write this, I just want to again point out that what is basically happening here is these calls are adding these functions that you've defined into an internal array. And what Express does every time a request comes in is just loop over that array, calling a function, and seeing whether or not that function fully handles the response or not.

[00:02:07]
If so, then it's done. If not, it moves on to the next one in the for loop, and the next one and the next one. So it's a top down, the order that you define these routes matters. If you define a more general catch all route earlier and it's going to catch all of them and never get to the specific one.

[00:02:24]
So you end up defining these from specific back to general in terms of the ordering.
>> Kyle Simpson: Okay, so to accomplish our URL rewriting, our strategy is to take that wreck object that has a wrecked out URL and literally just replace that value with the new value that we want.

[00:02:46]
So if it said /index, then we want to make it say index.html. And if it said /about, we want wanna to make it say about.html. That way when it gets to Express' static file server, it's a correct URL and it knows to just go find the file. That's what we're gonna be doing.

[00:03:05]
So again, just to save the time, I'm gonna go ahead and copy this, these if else statements. These are basically the equivalent of the static aliases that we used in the previous expression, I mean the previous exercise. So we're checking to see if it's index, and then we're gonna say /index.html.

[00:03:26]
If it's JS we don't need to do any rewriting, so we just immediately call next without doing any other processing. If it's one of these, this is where we switch it from /about to about.html. And then finally we say req.url is for a for.html in the last case.

[00:03:56]
All right, so,
>> Kyle Simpson: Save this.
>> Kyle Simpson: Then we'll close our define routes function, we're calling define routes. So hopefully now we should be able to start up our ex6. Let me do one thing before we start it up. Let's update our HTML file so that we can tell that it's serving up that new thing.

[00:04:48]
>> Kyle Simpson: Now we're in exercise six, and if we fetch the records, it still grabs the records. So we've got Express doing the same thing that our previous routing logic was doing.
>> Kyle Simpson: Notice it's coming back right away cuz I don't have that delay. We could go back to Express and put that delay in, and that would be right here.

[00:05:13]
>> Kyle Simpson: await delay(1000)
>> Kyle Simpson: Now we have 1,000 millisecond delay.
>> Student: Yeah, so what's the benefit then of using Express?
>> Kyle Simpson: The large community behind it, tons of documentation, huge ecosystem of contributed plugins for it. And just generally it's almost on every job posting that's ever been written for Node, that they want you to know Express.

[00:05:55]
>> Student: There are alternatives though?
>> Kyle Simpson: There are many, many alternatives, there are lots and lots of them. It's just that Express was kind of the first big one that got hugely popular. And probably for at least the next decade people are gonna be talking about and still using it.

[00:06:09]
But yeah, there's tons and tons of other ones out there. Express is not, I wouldn't necessarily call Express a framework, it's a little more like a library. But there's lots and lots of frameworks on top of Express, and actually Express was on top of another thing which is even more low level called Connect back in the day.

[00:06:30]
That was kind of the original thing and then Express was kind of a framework. And now you have much more sophisticated things on top of it from lots of companies like media and sales and who knows. It's a thousand others out there that do Node frameworks, loop back from the folks at Strong Loop and IBM, there's a ton of those out there.

[00:06:53]
Yes.
>> Student2: Also just mention that Scott Moss has an entire course on API design with Node using Express, which covers lots and lots of different things from authentication to using. He even has like a graph QL, interpret graph QL which uses Apollo server through Express. So Express has a lot of adapters for other types of tooling and what not.

[00:07:22]
>> Kyle Simpson: Yeah, absolutely. So that'd be a great place to go if you're more curious about Express. I'm sure he'll tell you a lot more official information about it. I've used Express a handful of times ever. And the vast majority of times that I make my own Node servers, I just end up falling back to my own routing logic cuz I don't end up needing a lot of that really sophisticated stuff for most of what I do.

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