This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const sqlite3 = require('sqlite3')
const db = require('./db.js')
const fastify = require('fastify')({
logger: true
})
fastify.get('/', async (request, reply) => {
return {hello: 'world'}
})
fastify.get('/users', (request, reply) => {
db.all('SELECT id, email, channel_id FROM users', (err, data) => {
if (err) return {error: err}
reply.send({data})
})
})
fastify.get('/users/:id', (request, reply) => {
const {id} = request.params
// console.log(request.params)
// reply.send({id})
db.get(`SELECT * FROM users WHERE id = "${id}"`, (err, data) => {
if (err) return {error: err}
console.log(data)
reply.send({data})
})
})
fastify.get('/channels', (request, reply) => {
db.all('SELECT * FROM channels', (err, data) => {
if (err) reply.send({error: err})
reply.send({data})
})
})
fastify.get('/channels/:id', (request, reply) => {
const {id} = request.params
db.get(`SELECT * FROM channels WHERE id = "${id}"`, (err, data) => {
if (err) return {error: err}
reply.send({data})
})
})
fastify.get('/tracks', (request, reply) => {
db.all('SELECT * FROM tracks LIMIT 5', (err, data) => {
if (err) reply.send({error: err})
reply.send({data})
})
})
fastify.get('/tracks/:id', (request, reply) => {
const {id} = request.params
db.get(`SELECT * FROM tracks WHERE id = "${id}"`, (err, data) => {
if (err) return {error: err}
reply.send({data})
})
})
const start = async () => {
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
// Run the server!
start()