Node backend for Angular using Nest JS.

Naimish Verma
Jul 1, 2019 · 3 min read

As per the official documentation of Nest JS, it is a framework for building efficient, scalable Node.js server-side applications. It is built with the full support of typescript and combines various features like Object Oriented, Functional and Reactive Programming.

Nest JS is inspired a lot by Angular, which is the very popular frontend Javascript framework for creating single page client-side applications.
It uses various features like dependency injections, modules, decorators and other such features which are quite frequently used in Angular. Nest JS enables the construction of fast, testable, and extensible backend applications.

Nest JS can easily be integrated with the existing Angular app so that your backend feels just like your frontend. The ng add command provides the bunch of schematics with help of ng-universal package which smoothly integrates the nest js in existing angular app.

Step 1-

Navigate inside your existing angular app and run the following command

ng add @nestjs/ng-universal

Step 2-

What this command will do is that it will install the Nest js and configure it for your angular app behind the hood. It will create the server folder at the root level of your application which includes the application level module and a main.ts file which is responsible for bootstrapping your nest application.

The main.ts file looks something like this-

enableProdMode();

async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix('api');
await app.listen(4200);
}
bootstrap();

It will bootstrap the application module and will automatically add the api prefix for incoming HTTP requests and will configure the app to listen to the incoming requests at the port 4200.

Nest JS provides the bunch of functionalities like controllers, services, middlewares, exception filters, support to web sockets, Graph QL and much more.

Just like Angular CLI, which uses ng generate commands to generate the file automatically, Nest CLI also uses nest generate commands to generate its required files.

Demo-
To test this, let us generate a simple controller which will handle the GET request at localhost:4200/api/greet and will return a JSON response with Hello World as a greeting.

Step 1- From inside the angular app, run the following commands-

cd server
npm install -g @nestjs/cli
nest generate controller greetings

These commands will install the Nest CLI globally and will create the greetings controller and will update the controllers' array in app.module.

The code of greetings controller looks something like this

import { Controller, Get } from '@nestjs/common';

@Controller('greet')
export class GreetingsController {
@Get()
greet() {
return { greeting: 'Hello World' };
}
}

Step 2- To test this run npm run serve.It will serve your angular app and your nest app at the same port i.e. 4200. The routing of the nest app will be prefixed by api .

For more details, dive deep into the official documentation of Nest JS-
https://docs.nestjs.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade