Skip to content

Latest commit

 

History

History
216 lines (139 loc) · 2.87 KB

02-Creating-a-REST-API-application.md

File metadata and controls

216 lines (139 loc) · 2.87 KB

[Kamil Mysliwiec, Mark Pieszak] NestJS Fundamentals Course [ENG, 2020]


02 Creating a REST API application


005 Prerequisite Install Insomnia

https://insomnia.rest/download/


006 Running NestJS in Development Mode

$ npm run start:dev

007 Creating a Basic Controller


$ cd app/api
$ nest generate controller coffees

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/flavors

returns

This action returns all coffees

008 Use Route Parameters


$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/123

returns

This action return #123 coffee

009 Handling Request Body Payload


010 Response Status Codes


011 Handling Update and Delete Requests


012 Implement Pagination with Query Parameters


$ curl \
    -H "Content-Type: application/json" \
    -X GET 'http://localhost:3000/coffees/flavors?limit=20&offset=10'

Returns:

This action returns all coffees. Limit: 20, Offset: 10

013 Creating a Basic Service


$ nest generate service coffees

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/1 \
    | python3 -m json.tool

returns

{
    "id": 1,
    "name": "Shipwreck Roast",
    "brand": "Buddy Brew",
    "flavors": [
        "chocolate",
        "vanilla"
    ]
}


014 Send UserFriendly Error Messages


$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/2 \
    | python3 -m json.tool

Returns:

{
    "statusCode": 404,
    "message": "Coffee #2 not found",
    "error": "Not Found"
}

015 Encompass BusinessDomain in Modules

$ nest generate module coffees

$ curl \
    -H "Content-Type: application/json" \
    -X GET http://localhost:3000/coffees/1 \
    | python3 -m json.tool

returns:

{
    "id": 1,
    "name": "Shipwreck Roast",
    "brand": "Buddy Brew",
    "flavors": [
        "chocolate",
        "vanilla"
    ]
}


016 Introduction to Data Transfer Objects

$ nest generate class coffees/dto/create-coffee.dto --no-spec
$ nest generate class coffees/dto/update-coffee.dto --no-spec

017 Validate Input Data with Data Transfer Objects

$ npm install class-validator class-transformer
$ npm install @nestjs/mapped-types

018 Handling Malicious Request Data


19 Autotransform Payloads to DTO instances




Marley

Any questions in english: Telegram Chat
Любые вопросы на русском: Телеграм чат