|
| 1 | +import { Application, Router } from 'https://deno.land/x/oak/mod.ts' |
| 2 | + |
| 3 | +const env = Deno.env.toObject() |
| 4 | +const HOST = env.HOST || '0.0.0.0' |
| 5 | +const PORT = env.PORT || 3000 |
| 6 | + |
| 7 | +interface IBook { |
| 8 | + id: string; |
| 9 | + author: string; |
| 10 | + title: string; |
| 11 | +} |
| 12 | + |
| 13 | +let books: Array<IBook> = [{ |
| 14 | + id: "1", |
| 15 | + author: "Robin Wieruch", |
| 16 | + title: "The Road to React", |
| 17 | +}, { |
| 18 | + id: "2", |
| 19 | + author: "Kyle Simpson", |
| 20 | + title: "You Don't Know JS: Scope & Closures", |
| 21 | +}, { |
| 22 | + id: "3", |
| 23 | + author: "Andreas A. Antonopoulos", |
| 24 | + title: "Mastering Bitcoin", |
| 25 | +}] |
| 26 | + |
| 27 | +const searchBookById = (id: string): (IBook | undefined) => books.filter(book => book.id === id)[0] |
| 28 | + |
| 29 | +const getBook = ({ params, response }: { params: { id: string }; response: any }) => { |
| 30 | + console.log(params) |
| 31 | + const book: IBook | undefined = searchBookById(params.id) |
| 32 | + if (book) { |
| 33 | + response.status = 200 |
| 34 | + response.body = book |
| 35 | + } else { |
| 36 | + response.status = 404 |
| 37 | + response.body = { message: `Book not found.` } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const router = new Router() |
| 42 | +router.get('/books/:id', getBook) |
| 43 | +// .get('/books', getBooks) |
| 44 | +// .post('/books', addBook) |
| 45 | +// .put('/books/:id', updateBook) |
| 46 | +// .delete('/books/:id', deleteBook) |
| 47 | + |
| 48 | +const app = new Application() |
| 49 | + |
| 50 | +app.use(router.routes()) |
| 51 | +app.use(router.allowedMethods()) |
| 52 | + |
| 53 | + |
| 54 | +console.log(`Listening on port ${HOST}:${PORT} ...`) |
| 55 | +await app.listen(`${HOST}:${PORT}`) |
0 commit comments