Angular Nx Tutorial - Step 5: Add Node Application Implementing an API

The requests fail because the API has not been created yet. Using Nx you can develop node applications next to your Angular applications. You can use same commands to run and test them. You can share code between the backend and the frontend. Use this capability to implement the API service.

Add NestJS plugin to your workspace

Nx is an extensible framework with plugins for many modern tools and frameworks. To see some plugins, run nx list:

> NX Installed plugins: @angular-devkit/build-angular (builders) @nrwl/angular (builders,generators) @nrwl/cypress (builders,generators) @nrwl/jest (builders,generators) @nrwl/linter (builders,generators) @nrwl/storybook (builders,generators) @nrwl/workspace (builders,generators) > NX Also available: @nrwl/express (executors,generators) @nrwl/nest (executors,generators) @nrwl/next (executors,generators) @nrwl/node (executors,generators) @nrwl/nx-plugin (executors,generators) @nrwl/react (executors,generators) @nrwl/web (executors,generators) > NX Community plugins: nx-plugins - Nx plugin integrations with ESBuild / Vite / Snowpack / Prisma, with derived ESBuild / Snowpack / ... plugins. @codebrew/nx-aws-cdk - An Nx plugin for aws cdk develop. @rxap/plugin-localazy - An Nx plugin for localazy.com upload and download tasks. ...

Add the dependency:

yarn add --dev @nrwl/nest
List plugins

When installing @nrwl/nest, it also automatically added @nrwl/node for you. Run npx nx list @nrwl/nest and npx nx list @nrwl/node to see what those plugins provide.

Create a NestJS application

Run the following to generate a new Nest application:

npx nx g @nrwl/nest:app api --frontendProject=todos

Nx asks you a few questions, and, as with the Angular application, the defaults work well here.

After this is done, you should see something like this:

myorg/ ├── apps/ │ ├── todos/ │ ├── todos-e2e/ │ └── api/ │ ├── src/ │ │ ├── app/ │ │ │ ├── app.controller.ts │ │ │ ├── app.controller.spec.ts │ │ │ ├── app.module.ts │ │ │ ├── app.service.ts │ │ │ └── app.service.spec.ts │ │ ├── assets/ │ │ ├── environments/ │ │ │ ├── environment.ts │ │ │ └── environment.prod.ts │ │ └── main.ts │ ├── jest.config.ts │ ├── proxy.conf.json │ ├── tsconfig.app.json │ ├── tsconfig.json │ └── tsconfig.spec.json ├── libs/ ├── tools/ ├── angular.json ├── nx.json ├── package.json └── tsconfig.base.json

The apps directory is where Nx places anything you can run: frontend applications, backend applications, e2e test suites. That's why the api application appeared there.

You can run:

CommandDescription
npx nx serve apiserve the application
npx nx build apibuild the application
npx nx test apitest the application

Open apps/api/src/app/app.module.ts.

import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [], controllers: [AppController], providers: [AppService], }) export class AppModule {}

We recommend using the Nest framework when creating node applications. Nest is a powerful framework which helps develop robust node applications. You can also use Express or any node libraries with Nx.

In this case you have an application that registers a service and a controller. Services in Nest are responsible for the business logic, and controllers are responsible for implementing Http endpoints.

Update apps/api/src/app/app.service.ts:

import { Injectable } from '@nestjs/common'; interface Todo { title: string; } @Injectable() export class AppService { todos: Todo[] = [{ title: 'Todo 1' }, { title: 'Todo 2' }]; getData(): Todo[] { return this.todos; } addTodo() { this.todos.push({ title: `New todo ${Math.floor(Math.random() * 1000)}`, }); } }

Next, update the controller to invoke the service:

import { Controller, Get, Post } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get('todos') getData() { return this.appService.getData(); } @Post('addTodo') addTodo() { return this.appService.addTodo(); } }

In a new terminal window, serve the API.

npx nx serve api

The API starts running on port 3333.

What's Next