These are the instructions for getting the backend to run:
We use NestJS for that. https://docs.nestjs.com/
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Under the hood, Nest makes use of Express, but also provides compatibility with a wide range of other libraries (e.g. Fastify). This allows for easy use of the myriad third-party plugins which are available.
Nest provides an out-of-the-box application architecture which allows for effortless creation of highly testable, scalable, loosely coupled, and easily maintainable applications.
npm i -g @nestjs/cli
nest new backend
Start dev server with watch-refresh
cd backend
npm run start:dev
nest g controller safes
// Creates
CREATE /src/safes/safes.controller.spec.ts (478 bytes)
CREATE /src/safes/safes.controller.ts (99 bytes)
UPDATE /src/app.module.ts (399 bytes) // Adds SafesController to module
Add first method
@Get()
findAll() {
return 'This action returns all safes';
}
When a GET request is made to this endpoint, Nest will now return a 200 status code and the serialized JSON response, which in this case just an empty array. Why does that happen? Generally, we distinguish two different approaches to manipulate responses:
Postman:
Add specific GET method
@Get(':id')
findOne(@Param() params) {
console.log(params.id);
return `This action returns a #${params.id} safe`;
}
Postman:
@Post()
create() {
return 'This action adds a new safe';
}
TODO
But first (if you use TypeScript), we need to determine the DTO (Data Transfer Object) schema. A DTO is an object that defines how the data will be sent over the network. We could determine the DTO schema by using TypeScript interfaces, or by simple classes. Surprisingly, we recommend using classes here. Why? Classes are part of the JavaScript ES6 standard, and therefore they represent plain functions. On the other hand, since TypeScript interfaces are removed during the transpilation, Nest can't refer to them. This is important because features such as Pipes enable additional possibilities when they have access to the metatype of the variable.
export class CreateSafeDto {
readonly id: string;
readonly name: string;
}
export class Safe {
readonly id: string;
readonly name: string;
}
readonly because we should always try to make our functions as pure as possible.
Use the newly created schema inside the SafesController:
@Post()
async create(@Body() createSafeDto: CreateCafeDto) {
return 'This action adds a new safe';
}
nest g service safes/safes
// Creates
CREATE /src/safes/safes.service.spec.ts (449 bytes)
CREATE /src/safes/safes.service.ts (89 bytes)
UPDATE /src/app.module.ts (394 bytes) // Writes service to module
Add functionality:
private readonly safes: Safe[] = [];
create(safe: Safe) {
this.safes.push(safe);
}
findAll(): Safe[] {
return this.safes;
}
findOne(id: string): Safe {
return this.safes.filter(safe => safe.id === id)[0];
}
Here's a SafesService, a basic class with one property and two methods. The only new trait is that it uses the @Injectable() decorator. The @Injectable() attaches the metadata, thereby Nest knows that this class is a Nest provider. Notice that there is a Safe interface used above. We didn't mention it because the schema is exactly same as in the CreateSafeDto class which we have created in the previous chapter.
Add constructor and functions
constructor(private readonly safesService: SafesService) {}
@Post()
async create(@Body() createSafeDto: CreateSafeDto) {
this.safesService.create(createSafeDto);
}
@Get()
async findAll(): Promise<Safe[]> {
return this.safesService.findAll();
}
@Get(':id')
async findOne(@Param('id') id): Promise<Safe | HttpException> {
const foundSafe = this.safesService.findOne(id);
if (!foundSafe) {
throw new HttpException('Not Found', HttpStatus.NOT_FOUND);
}
return foundSafe;
}
Postman
{
"id": "1",
"name": "abc"
}
TODO next: validate if id is already in the safes array