39
loading...
This website collects cookies to deliver better user experience
Name of Route | Request Method | Endpoint | Result |
---|---|---|---|
Index | GET | /model |
returns list of all items |
Show | GET | /model/:id |
returns item with matching id |
Create | Post | /model |
creates a new item, returns item or confirmation |
Update | Put/Patch | /model/:id |
Updated item with matching ID |
Destroy | Delete | /model/:id |
Deletes item with matching ID |
create a folder for this exercise and navigate your terminal to that server.
let's create our two project
npm i -g @nestjs/cli
nest new n-practice
npm run start
which default runs on port 3000 (localhost:3000)npm install -g @foal/cli
foal createapp f-practice
npm run develop
which default runs on port 3001 (localhost:3001)@decorator
that designate the route each function belongs to.nest g controller posts
(creates src/posts/posts.controller.ts)foal generate controller posts
(create src/app/controllers/posts.controller.ts)import { controller, IAppController } from '@foal/core';
import { createConnection } from 'typeorm';
import { ApiController, PostsController } from './controllers';
export class AppController implements IAppController {
subControllers = [
controller('/api', ApiController),
controller('/posts', PostsController) // <---------------------
];
async init() {
await createConnection();
}
}
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
import { Controller, Get } from '@nestjs/common';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
// Our Controller for "/posts"
@Controller('posts')
export class PostsController {
@Get()
index(): Array<Post> {
return posts
}
}
import { Context, Get, HttpResponseOK } from '@foal/core';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
export class PostsController {
@Get('/')
index(ctx: Context) {
return new HttpResponseOK(posts);
}
}
import { Controller, Get, Param } from '@nestjs/common';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
// Our Controller for "/posts"
@Controller('posts')
export class PostsController {
@Get()
index(): Array<Post> {
return posts
}
@Get(':id')
// use the params decorator to get the params
show(@Param() params): Post {
const id = params.id
return posts[id]
}
}
import { Context, Get, HttpResponseOK } from '@foal/core';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
export class PostsController {
@Get('/')
index(ctx: Context) {
return new HttpResponseOK(posts);
}
@Get('/:id')
show(ctx: Context){
const id = ctx.request.params.id
return new HttpResponseOK(posts[id])
}
}
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
// Our Controller for "/posts"
@Controller('posts')
export class PostsController {
@Get()
index(): Array<Post> {
return posts
}
@Get(':id')
show(@Param() params): Post {
const id = params.id
return posts[id]
}
@Post()
// use body decorator to retrieve request body
create(@Body() body:Post):Post {
posts.push(body)
return body
}
}
import { Context, Get, HttpResponseOK, Post } from '@foal/core';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
export class PostsController {
@Get('/')
index(ctx: Context) {
return new HttpResponseOK(posts);
}
@Get('/:id')
show(ctx: Context){
const id = ctx.request.params.id
return new HttpResponseOK(posts[id])
}
@Post("/")
create(ctx: Context){
const body: Post = ctx.request.body
posts.push(body)
return new HttpResponseOK(body)
}
}
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
// Our Controller for "/posts"
@Controller('posts')
export class PostsController {
@Get()
index(): Array<Post> {
return posts
}
@Get(':id')
show(@Param() params): Post {
const id = params.id
return posts[id]
}
@Post()
create(@Body() body:Post):Post {
posts.push(body)
return body
}
@Put(":id")
update(@Param() params, @Body() body: Post): Post {
const id = params.id
posts[id] = body
return posts[id]
}
}
import { Context, Get, HttpResponseOK, Post, Put } from '@foal/core';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
export class PostsController {
@Get('/')
index(ctx: Context) {
return new HttpResponseOK(posts);
}
@Get('/:id')
show(ctx: Context){
const id = ctx.request.params.id
return new HttpResponseOK(posts[id])
}
@Post("/")
create(ctx: Context){
const body: Post = ctx.request.body
posts.push(body)
return new HttpResponseOK(body)
}
@Put("/:id")
update(ctx: Context){
const body: Post = ctx.request.body
const id = ctx.request.params.id
posts[id] = body
return new HttpResponseOK(posts[id])
}
}
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
// Our Controller for "/posts"
@Controller('posts')
export class PostsController {
@Get()
index(): Array<Post> {
return posts
}
@Get(':id')
show(@Param() params): Post {
const id = params.id
return posts[id]
}
@Post()
create(@Body() body:Post):Post {
posts.push(body)
return body
}
@Put(":id")
update(@Param() params, @Body() body: Post): Post {
const id = params.id
posts[id] = body
return posts[id]
}
@Delete(":id")
destroy(@Param() params):any {
const id = params.id
const post = posts.splice(id, 1)
return post
}
}
import { Context, Delete, Get, HttpResponseOK, Post, Put } from '@foal/core';
// Interface Defining the Shape of a Post
interface Post {
title: string,
body: string
}
// Array of Posts
const posts:Array<Post> = [
{title: "THe First Post", body: "The Body of the First Post"}
]
export class PostsController {
@Get('/')
index(ctx: Context) {
return new HttpResponseOK(posts);
}
@Get('/:id')
show(ctx: Context){
const id = ctx.request.params.id
return new HttpResponseOK(posts[id])
}
@Post("/")
create(ctx: Context){
const body: Post = ctx.request.body
posts.push(body)
return new HttpResponseOK(body)
}
@Put("/:id")
update(ctx: Context){
const body: Post = ctx.request.body
const id = ctx.request.params.id
posts[id] = body
return new HttpResponseOK(posts[id])
}
@Delete("/:id")
destroy(ctx: Context){
const id = ctx.request.params.id
const post = posts.splice(id, 1)
return new HttpResponseOK(post)
}
}