This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Intro to Express" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

Express is the most popular framework for building APIs and is very extensible. There are modules for routing, middleware, and lots of plugins to do whatever you would need for building APIs for REST and GraphQL.

Get Unlimited Access Now

Transcript from the "Intro to Express" Lesson

>> Scott Moss: So why express? Like I said, it's the go to for creating API's with node currently right now and you'll see in a minute why that is. Other frameworks are based off of Express which was based off Connect, so it's a pretty good choice, tons of plug-ins integrations.

[00:00:18] It's pretty much like I said, if you are using some third party service, I don't know let me, like you see, we're gonna be using GraphQL, they have an express plugin for it. They just went ahead and made one, than if we're using something like not name brand server framework, good luck, there's probably not gonna be a plugin for it.

[00:00:33] Or the thing that you made yourself, good luck, you gotta build the integration yourself. So Express pretty much has all the plugins integrations for anything interacting with server side APIs with note. Plenty of resources out there, their documentation is really legit. There's tons of blog posts and stuff out there, so you can pretty much rely heavily on that.

[00:00:53] Very easy to use, fast to develop and I find it very extremely easy to test, if you write tests. Some people here don't write tests, that's fine if you don't write tests, that's cool but when you do write tests, because you will eventually, Express makes it really easy to do that.

[00:01:09] Any questions on that, no? Okay, so some of the features, has declarative routing. Like, you can do exact routing, pattern matching, globs, parameter matching and pretty much anything you could think of with routing, you could pretty much do it with Express. So if you wanna throw a regex in there, you wanna throw some starts in there, you wanna throw the exact thing, you wanna throw some options, you could do it, it's pretty easy.

[00:01:36] Use the middleware functions to run serially on your request. We'll get into middleware but you can think of them as like, hooks before your actual endpoint gets invoked, right? You have the thing that is actually going to get the resource and before that you may have some functions that run serially.

[00:01:54] This function, then this function, then this function, this function, and then finally run the last function, so that is kinda like what middleware is. Powerful response options, you can pretty much send back whatever you want, and from JSON to static files, you can send back streams. You can stream directly from a database, you can redirect, set headers, pretty much everything you need from API, pretty flexible.

[00:02:17] It's database agnostic, doesn't care what database you are using, because it doesn't hook in to the database. So, you can use whatever you want, as many databases as you want, however you want to use them, doesn't really matter. And then there's tons, tons, tons of other stuff that Express gives us.

[00:02:35] Any questions on Express? No, all right, so getting started, this is like the easiest Express app you can probably make right here. That's express right there, you can see, so if you've never used node before, some of the stuff might be like, well is what this require stuff.

[00:02:58] That's like the quote of like a script type on the server side, using require is like I wanna load in this other JavaScript file, right? So once you npm install it, or you all install it wherever, you'll have Express and then you make a new app. You register a route for the index which is a slash and then you respond to that route by using the JSON method and then you list it on whatever port you want, and that's it.

[00:03:22] So now you're able to do a get request to the index, and receive a JSON object with okay true on port 3000. That's the most basic Express app you can make right there. Any questions on how simple that is? Or anybody wanna ask some questions about what's going on here?

[00:03:40] This looks confusing, is there something here you've never seen before? No, okay.