// user.controller
import { Controller, Get, Query } from '@nestjs/common';
import { UserService, GetNumber } from './user.service';
@Controller('user')
export class UserController {
constructor(private readonly UserService: UserService) {}
@Get()
getUsers(): any {
return {
code: 0,
data: [],
msg: '请求成功',
};
}
@Get('/range')
getNum(@Query() query: { num: string }): GetNumber {
return this.UserService.getNumber(query.num);
}
}
//user.service
import { Injectable } from '@nestjs/common';
export interface GetNumber {
code: number;
msg: string;
data: string[];
}
@Injectable()
export class UserService {
getNumber(num: string): GetNumber {
const regExp = /^[0-9]*[1-9][0-9]*$/;
if (!regExp.test(num)) {
return {
code: 5000,
msg: '请求失败,请输入正整数',
data: [],
};
}
const arr: string[] = [];
for (let i = 1; i <= Number(num); i++) {
arr.push(String(i));
}
return {
code: 0,
msg: '请求成功',
data: arr,
};
}
}