Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Express Q&A" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will discusses the goals of the course and then explains what the job of Express and other middleware is within Node.


Transcript from the "Express Q&A" Lesson

>> Will Sentance: So I hope your, I don't know, maybe I want you to be bored at the end of this. I want you to be bored because I want you to feel wow, that's all Node is? That same model again and again and again? Cuz it's gonna make all of Node trivial to you, meaning you are then gonna be capable and abled to go and tackle any Node feature.

Because they're all this model and you just got a spot which bits which, why is Michael whispering right now?. He moved the mike away so we know it's inappropriate that we're certain of. We know he does want to be on record for what he's saying. Okay, so we don't necessarily but MiddleWeb, we'll talk MiddleWeb a bit later on.

It's gonna turn out, and you'll see it right here that we're going to need to look at the inbound message and say, if they ask me about page. Send about the about page. If they ask for the help page, send back the help page. There's a lot of if else is.

And, like anything I react, any task that's done a lot, hopefully some developers helped you out by pre-writing code to deal with that task. So for example the task of, if they call to this page, send back this and this page, you've got hundreds of pages. You wanna write if else for everything one of them?

No. Express what it is, is gonna turn out to be pre-written JavaScript code that is a bunch of if false statements, will be part of it that. But if else statements, if user ask of this page, send back that and is gonna do that in one line for you.

So they're all your web pages arrays to be sent back, whatever they are whatever they asked for. The thing they do more interestingly, remind me after this session to tell you what they do more interesting, the MiddleWeb pattern is fantastic. It's a way, all its job is really is, I'll give you a preview right now.

Our only job I've just said in Node is to look at that object that Node creates for us, with all the inbound data and analyze it. Analyze, are you logged in is the header got logged in cookie? Analyze what does the the exact tweet we want to send back is.

I don't know. Analyze, well, not just is the user logged in, let me go and get that user's data at that point from the database, like a lot of stuff basically, on that object need to go through one by one. Do I wanna do that all in one giant function do an incoming?

Nah, so instead I wanna break it up into individual functions. The things I wanna look out on that object, step one, check what user if user logged in. Step two, get users days from data base. Once I know they logged in by looking at the object headers. Step three, check that you URL they want, URL property.

Don't do that all at once function, do it in series of functions. The MiddleWeb passenger says, take that object that's gonna be all create by Node, and pass it through function, through function, through function doing something to it at each stage and then returning it back out. Implicitly returning it back out at the end of using the function to do the next task.

It's only job is to modularize what you want to dummies put into separate pieces, separate chunks rather than one giant function, what you want to do to the inbound object to look at it and send back the right data. It's a do that in steps, each of those steps is an individual function.

That is a MiddleWeb pattern. We will come to that again a bit more in a second but expresses his pre-written jobs code that does what we're doing here. Okay. Thoughtful question Michael.

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