My first question is, what is your purpose for using NestJS?
You can think of NestJS and json-server as "similar" in their goal; you would use one OR the other. Not both.
- If your goal is just to mock data and server, then you have everything you need with json-server. You wouldn't need NestJS.
- If what you are looking for is to mock data to retrieve instead of creating a database, you can simply create a simple object in NestJS and retrieve data from there.
For the latter, it might look something like this (not tested):
// app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from '../services/app.service';
@Controller('api')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/users')
findAllUsers(): Promise<any> {
return this.appService.findAllUsers();
}
}
// app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
private getMockData() {
return {
users: [
{
name: 'john',
email: 'john@doe.com',
},
{
name: 'jane',
email: 'jane@doe.com',
},
],
};
}
findAllUsers(): Promise<any> {
return new Promise(function (resolve, reject) {
const data = this.getMockData();
if (data.users.length > 0) resolve(data.users);
else reject('No users found');
});
}
}
Now you would just have to do the request GET <host>/api/users
where the host will be something like http://localhost:8080
in your local machine.