Check out a free preview of the full Production-Grade Angular course
The "Mocking APIs with Nest" Lesson is part of the full, Production-Grade Angular course featured in this preview video. Here's what you'd learn in this lesson:
Lukas introduces Nest, a progressive Node.js framework that is heavily inspired by Angular, which allows developers and teams to create testable and scalable applications. How to install and use Nest to build mock APIs is also discussed in this segment.
Transcript from the "Mocking APIs with Nest" Lesson
[00:00:00]
>> We've covered how to run multiple applications within a single model repo. Then we went on to see how to actually consume a single component across multiple applications. What I want to do now is just take a brief moment and I want to introduce nest ITO the mix.
[00:00:23]
So we're gonna walk through a few slides. And then we're just gonna get back to what we were doing, which is programming. And I would have to say that, in my opinion, nest is probably one of the best things that ever happened. To angular, and if you are not familiar with NAS, then you are in for a treat.
[00:00:53]
So, we already saw how to mock an API with JSON server. The challenge with that is You will never actually use JSON server to do anything real. It is merely just a placeholder until you have an opportunity to get a quote unquote real server in place. Now, the difference is we're making this transition and the reason why I'm talking about this in the context of an.
[00:01:25]
Angular workshop is going to become very apparent very quickly and anybody who's worked with nest, just bite your tongue don't spoil the movie. Because I think if you haven't heard of nest, it's pretty crazy. So Mark API's. Think of this as the Hello nest module. And so, on their website it provides an out-of-the-box application blah blah blah blah blah you just go straight to.
[00:01:59]
The bottom, and it says the architecture is heavily inspired by Angular. Now there are some things that I really like. It certainly comes with architecture. So if you've ever used express, there's not a lot of architecture that comes with that. But ness was written to be highly testable, scalable.
[00:02:19]
Loosely coupled and easily maintainable. So pretty much at that point, I just basically held my hand out to Camille who wrote I said, You had me at hello, take my money. And so the architecture is heavily inspired by Angular. So we have nest,we have Angular. And let's walk through this real quick.
[00:02:38]
What do we love about Angular? Well, it uses TypeScript My favorite thing about it's a reactive framework, so under the hood yes. I love love love observables. Great tooling. Leverages dependency injection, I think this is a really, really good pattern. Clean abstractions, and if you're comfortable with Angular, you already know Nest.
[00:03:14]
So allow me to prove my point. This is And what, what are we looking at, Is it Angular? Is it nest, well, it's nest, but it looks just like Angular. Also nest just Like Angular, in so much. Has anybody ever seen this? It's not mg module, but it is module, and now one more thing It comes with a ton of functionality.
[00:04:08]
So you get swagger, dedication with passport. Get entities with TypeORM, GraphQL support, Realtime Data with Socket.IO. Microservice support and Fastify Support And so I'm not going to go through every piece of this. This will be in the slides that I'll upload. So we're gonna press pause here and One second.
[00:04:46]
Every time I do that, I just waive putting that up there. So we're going to do instead of going through the slides, you can download the PDF. You go through it I talked about it's cool. I think the better thing to do is let's hop into the code.
[00:05:02]
And let's generate some stuff. So from the command line, let me open up a new one. All right now. From the command line we're gonna do a few things. I'm gonna add this uuid package. And when this is done, As always add something in, and then it just starts getting angry.
[00:05:55]
Anyways there we go What I'm just gonna pause these for a second. So this can do its thing, all right. I'm gonna add one more here and this is gonna be nest js. Mapped types. Now, if this workshop was a movie, this is what we would call foreshadowing and character development because.
[00:06:40]
This is about to get awesome. From here from the command line. Let's generate some stuff. And I used to do this kind of piece by piece, and recently within the last couple weeks By last couple weeks,. I mean, October of 2020 just to kind of timestamp that, so it's not 2022 and they're like this just came out.
[00:07:12]
They introduced the schematics for resources. And so what I can do here Is I can set the type, which is gonna be rest, but you can do graph QL. We're gonna do crud if it's true and we're gonna set the source route to apps API source. And let's let This fly all right, from here.
[00:07:46]
Let's look into our code. We'll just kind of close all this down, and we'll hop up into our API. And let's see what this looks like. So now we have a widgets folder. And within the widgets folder, we have our module and we have a controller. And so in a controller when you decorate a method This now becomes your endpoint.
[00:08:26]
Login, so now it's like this is your endpoint up here, widgets, but then these are the verbs. And so the verbs, your restful verbs get routed to these particular functions, which then if you look in your service. Then it's doing some stuff in here. And so now we have basically in about 30 seconds generated 90% of a REST API.
[00:08:58]
The difference being I would put this in production Not I would, I have. Nest is amazing, it's built on top of Express. But using the schematics inside of Nx, I'm able to generate all this stuff for free. Ready to go.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops