Setting up our Nest.js application

Nest is a Node.js framework that's used to build scalable, efficient, and reliable server applications. It shares some similarities with the Angular framework, even though they solve completely different problems. For example, in terms of TypeScript, similar dependency injection techniques and decorators are used for its main building blocks. These similarities between Nest and Angular makes them a perfect fit for each other.

Let's create an application using Nest.js. Instead of installing @nestjs/cli globally using npm, let's just use npx to make use of @nestjs/cli and create a new Nest.js application. Run the following command in the CLI:

> npx @nestjs/cli new es-api

This should create a Nest.js app in the es-api folder. Let's add Mongoose to our application. Mongoose is used to connect to MongoDB.

Let's add the dependencies for mongoose in our root folder for es-api:

> yarn add mongoose @types/mongoose

Since this book does not focus on Nest.js, we will not go into detail about how to create modules, services, and controllers in Nest.js. The good part about Nest.js is how similar it is to Angular.

Let's add the products and database folders from the Chapter 5/es-api/src folder (https://github.com/PacktPublishing/Angular-Projects/tree/master/Chapter05/es-api/src) to our src folder and add ProductsModule to AppModule, as follows:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';

import
{ AppService } from './app.service';
import { ProductsModule } from './products/products.module';

@Module({
imports: [ProductsModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

We will also need to enable Cross-Origin Request Sharing (CORS) so that our Angular app will be able to access this API in the application. The CORS mechanism makes sure that the API's consumption by a client can only happen when the server allows it to. 

We can enable CORS in main.ts in our Nest.js application as follows:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: '*',
});
await app.listen(3000);
}
bootstrap();

Now, when you want to run this app using the npm start command from inside the es-api folder, you should be able to use the API to get products. Use the following URL to check this: http://localhost:3000/products.

If you check DatabaseModule, you will find a MongoDB connection string starting with mongodb://. You can replace it with your local MongoDB or, if you don't have one, use mlab.com to create your own instance of MongoDB.

Now that we've got our API working by using the Nest.js framework, let's focus on building our client application using Angular.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.9.236