Serverless with AWS Lambda Routing Serverless with Express
Transcript from the "Routing Serverless with Express" Lesson
>> Scott Moss: A few things we need to clean up is, one, what we'll have to do is, we'll have to like I say consolidate our function so what we'll do next is, I'm not going to delete anything because I'm going to leave that up to you. I'm just going to make another function here.
[00:00:16] And I'm going to call this one God, API's already taken. It's such a good name. [LAUGH] I don't want to rename it because then it gets confusing. You know what? I'm just gonna reuse this API1. So I'm gonna reuse this API1. And then, for its HTTP event, I'm just gonna put
>> Scott Moss: Any, which basically means any verb, so get, post, put, delete, whatever, to this path. So any verb to this path, that's the short hand method. So I wanna do that. Also want to be able to proxy to any
>> Scott Moss: Thing you might put here, and then I'll put this plus, which basically means, I've gotta wrap this in the string.
[00:01:08] This is saying, any verb to root/ whatever this is, it can be anything, that's what this is saying. So it doesn't matter what's coming after here. It can be as many slashes, just redirect all that. I'll put this in a string because the reason a combination of this syntax, like a verb and a template thing here.
[00:01:34] So you gotta wrap it in a string.
>> Scott Moss: I think that's just a YAML thing. So this is just saying, this api function is not going to respond to anything coming in here and anything coming in here. So I can just kinda comment these out.
>> Scott Moss: And let's test this.
[00:01:54] So I go to api, looks like it's sending back hello, so let me stop that.
>> Scott Moss: And I'm gonna do yarn dev.
>> Scott Moss: Serverless.yml, I think I gotta check out to the new branch, actually. git co lesson-3, yeah, check out to the new branch.
>> Scott Moss: Let me copy this stuff.
[00:02:19] Copy the goods.
>> Scott Moss: Let me check it out.
>> Scott Moss: Right here.
>> Scott Moss: Okay.
>> Scott Moss: All right, so now that we've got that. Yeah, this one was down here.
>> Scott Moss: Cool, so now we're gonna run dev and hopefully that doesn't break.
>> Scott Moss: There we go. All right, so you can see right now we've got routes for API, ANY / and then param everything.
[00:03:04] So let's test this out. If I go to http :4500 / hey/there.
>> Scott Moss: Looks like it's working. I definitely didn't make that route. So looks like it's going there.
>> Scott Moss: Perfect. So our proxying is working fine. So it looks like we're forwarding everything. And just to double check, let's issue a different method like post here.
[00:03:32] See if that works. Yep, that works, too. What about, PUT
>> Scott Moss: Any questions on that? How to set that up. Again most of the stuff is in the service docs, all this is a service.
>> Scott Moss: Pretty simple, right? The next thing we're gonna do is we need to find a way to do some routing in here.
>> Scott Moss: Yeah, you could just look at the event object, right? And just look at the path parameters, and you can set up your own state machine that you use for a router. I mean, it's pretty simple. You could just have keys in here that are like this/route.
>> Scott Moss: And then run this method which is the controller.
[00:04:22] So, there's a couple things you can do here but that's kind of garbage. [LAUGH] So, you probably wanna use like a regular router. Or I'm sorry, you wanna use a router that's already got some features built into it like express. So that's what we wanna do. So there's a couple of packages we wanna use.
[00:04:38] And you could look in the packets.json. There's one. Obviously there's express. And then there's this serverless HTTP. So those are the two packages we're gonna be using to create our express server. So first thing first is what we'll do Is we'll just say express equals require express. And who here has used express?
[00:05:02] Okay, so if you haven't used express it's so easy like you won't, you don't need to learn it, just watch what I'm doing and ask questions. And then we'll have this server, I'm just gonna call it HTTP.
>> Scott Moss: Cool, so now we got that, and what we can do now is, we can make an express app.
[00:05:30] So let's just say, app = express
>> Scott Moss: And then we'll make some routes, app.get/ just this route. And we'll just register a handler here, so request response. And we'll just do a response.json that says
>> Scott Moss: Okay, so we'll do that.
>> Scott Moss: But this isn't going to work because http kind of like, http creates this handler for us.
[00:06:07] So cuz right now it's like okay cool. I got this route but like what do I do with this stuff, right? So what you're gonna do is you're gonna take this out of the handler. You're gonna get rid of all of this. And instead, there's gonna make your app appear in this closure.
>> Scott Moss: And then now, all you're gonna export is HTTP wrapping your app. And then that's it. Now, you have an express app On service. A service express app. So if you have express like this is literally the easiest transition to Alanda. It's pretty ridiculous. So let's test this.
[00:06:45] Maybe I didn't do it right. But let's see.
>> Scott Moss: Cool, so it looks like it's still starting. I registered a route for route. So let's check that out. It was a git request. So, 4500, git, message okay. Looks like it's good. Let's test out these other ones, like a PUT, boom can't do that, because I didn't register that with express.
[00:07:11] So, server has proxied it correctly. But then Express was like, no, can't do that, so it didn't really work out, so pretty simple. So now, at this point, you're really just doing everything that Express can do, without having to start a server on some port, right, cuz you're just a service function.
[00:07:30] And it's stateless, so this is only gonna happen per request, so it's pretty cool. Any questions on this? Yes.
>> Speaker 2: So as an Express programmer, basically you have, when I think about this code running on a standalone server, there's an instance of Express listening at all times.
>> Scott Moss: Yep.
>> Speaker 2: How does that reconcile, maybe it's just my brain that's not working through this. How does that reconcile with idea of Alanda which does its job and then shuts down?
>> Scott Moss: Yeah, okay, so there's two parts to the express route, right? You have the routing and the controllers in the middleware, that's the framework.
[00:08:17] And then you have the networking side of it, which is actually the server. Just think of Express without the server. That whole part that's always on that's list thing, it's gone. Because in Express, when you do something like what is it, app. Listen?
>> Speaker 2: Yeah, but you didn't do that cuz you have that in HTTP.
>> Scott Moss: Right, that's what's doing it.
>> Speaker 2: That makes better sense.
>> Scott Moss: Yeah.
>> Speaker 2: Okay.
>> Scott Moss: Because express built in actually uses the native HTTP method in node. So when you it call listen, it's actually just starting, it's doing http.createserver, whereas this, it's kinda proxying that. It's poly-filling, it's satisfying whatever Express needs, but it's not actually functional.
[00:08:57] It's really actually providing functional. I mean, this is just returning a function, right? So we could look at this. Let's just look at it so maybe that'll make more sense. So, man, this keyboard is so sticky. Let's do this. So that's the handler,
>> Scott Moss: Right, so this is just gonna return the function and in fact we'll do this.
>> Scott Moss: My god
>> Scott Moss: Actually, let me clean this up, it's bothering me. There you go. Okay, so yeah, this is gonna return a function and we can see what this function is.
>> Scott Moss: We'll just execute that, go back here. It's just gonna say function. Let me do, I can do that, I don't know why I thought that was gonna do something different.
>> Scott Moss: There we go. Cool.