简体   繁体   English

为什么端点不能从导入的 NESTJS 模块中工作

[英]Why endpoints not working from imported NESTJS module

tell me please why appController working and itemsController no (from imported module)请告诉我为什么 appController 工作而 itemsController 没有(来自导入的模块)

I learn nestjs and i did it according to documentation.我学习了nestjs,并根据文档进行了操作。 This controller working its uncomment endpoint.这个 controller 工作其取消注释端点。

import {Controller, Get} from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor() {}
  
  // @Get()
  // getHome():string {
  //    return 'Hello world!';
  // }
  
}

itemsController.ts - working if change appController.ts on this itemsController.ts - 如果更改 appController.ts 则工作

import {Controller, Get, HttpException, HttpStatus, Param, Post, Res} from "@nestjs/common";
import {Response} from "express";
import {ItemsService} from "./items.service";
import {ItemDto} from "./dto/item.dto";

@Controller()

export class ItemsController {
    
    constructor(private readonly itemsService: ItemsService) {}
    
    @Get()
    getAll(@Res({passthrough: true}) res: Response):string | object {
        
        const items = this.itemsService.getAll();
        
        if(!!items.length) {
            res.status(HttpStatus.OK);
            return new HttpException({
                items: items
            }, HttpStatus.OK).getResponse();
        }
        
        res.status(HttpStatus.INTERNAL_SERVER_ERROR);
        
        return new HttpException({
            items: 'Items length: ' + items.length,
            status: HttpStatus.INTERNAL_SERVER_ERROR
        }, HttpStatus.INTERNAL_SERVER_ERROR).getResponse();
        
    }
    
    @Post()
    create(@Param() params):ItemDto {
        return this.itemsService.create(params);
    }
    
}

Test jest working:测试笑话工作:

import { Test } from '@nestjs/testing';
import { ItemsController } from './items/items.controller';
import { ItemsService } from './items/items.service';
import * as request from 'supertest';
import { INestApplication } from "@nestjs/common";

describe('ItemsModule', () => {
    let itemsController: ItemsController;
    let itemsService: ItemsService;
    let app: INestApplication;
    
    beforeEach(async () => {
        const moduleRef = await Test.createTestingModule({
            controllers: [ItemsController],
            providers: [ItemsService],
        }).compile();
        
        itemsService = moduleRef.get<ItemsService>(ItemsService);
        itemsController = moduleRef.get<ItemsController>(ItemsController);
        
        app = moduleRef.createNestApplication();
        await app.init();
    });
    
    describe('End-points', () => {
        
        it('/GET Status 200', () => {
            
            return request(app.getHttpServer())
                .get('/')
                .expect(200)
                .expect({
                    "items": [
                        {
                            id: '0',
                            title: '',
                            message: ''
                        }
                    ]
                });
            
        });
        
        it('/GET Status 500', () => {
            
            return request(app.getHttpServer())
                .get('/')
                .expect(500)
                .expect({
                    items: 'Items length: 0',
                    status: 500
                });
            
        });
    });
});

I pushed all on github, you can see all code我都推到github上,你可以看到所有代码

After looking at your source code, you're missing the @ for @Module() in your ItemsModule查看您的源代码后,您的ItemsModule中缺少@@Module()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM