Lesson 3Nx Add Backend app
In this section, we add to our workspace a backend app and a backend library. For this workshop, I decided to use Nestjs like backend framework, but you could choose your favorite from Express, (Nestjs)[https://nx.dev/latest/node/nest/overview], Next.js or Gatsby.
Now let's start to add our new Nestjs Application, but before adding it, we have to install e @nrwl dependence specialized to manage nestjs framework.
> npm install --save-dev @nrwl/nest> npm run nx -- generate @nrwl/nest:application --name=hero-api> npm run nx -- generate @nrwl/nest:library --name=controllers --directory=hero> npm run nx -- generate @nrwl/nest:controller --name=hero-controller --project=hero-controllers --directory=lib/controllers
libs/hero/controllers/src/lib/controllers/hero-controller/hero-controller.controller.ts
import { Controller, Get } from '@nestjs/common'export type HeroDTO = {id: stringname: stringimage: stringpowerstats: {intelligence: numberstrength: numberspeed: numberdurability: numberpower: numbercombat: number}}@Controller('hero')export class HeroControllerController {private heros: HeroDTO[] = [{id: '732',name: 'Ironman',powerstats: {intelligence: 100,strength: 85,speed: 58,durability: 85,power: 100,combat: 64,},image: 'https://www.superherodb.com/pictures2/portraits/10/100/85.jpg',},{id: '332',name: 'Hulk',powerstats: {intelligence: 88,strength: 100,speed: 63,durability: 100,power: 98,combat: 85,},image: 'https://www.superherodb.com/pictures2/portraits/10/100/83.jpg',},{id: '149',name: 'Captain America',powerstats: {intelligence: 69,strength: 19,speed: 38,durability: 55,power: 60,combat: 100,},image: 'https://www.superherodb.com/pictures2/portraits/10/100/274.jpg',},]@Get()public get(): Promise<HeroDTO[]> {return Promise.resolve(this.heros)}}
apps/hero-api/src/app/app.module.ts
import { Module } from '@nestjs/common'import { HeroControllersModule } from '@flowing/hero/controllers'import { AppController } from './app.controller'import { AppService } from './app.service'@Module({imports: [HeroControllersModule],controllers: [AppController],providers: [AppService],})export class AppModule {}
> npm run nx -- serve hero-api
Navigate to: