Skip to content

Commit feb2e74

Browse files
committed
Add mock server
1 parent 7893d77 commit feb2e74

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

test/deps.ts

Whitespace-only changes.

test/main.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)