-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (37 loc) · 987 Bytes
/
index.js
File metadata and controls
43 lines (37 loc) · 987 Bytes
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
require('dotenv').config()
const express = require('express')
const app = express()
const port = 8000
// using sqlite
// const knex = require('knex')({
// client: 'sqlite3',
// connection: { filename: process.env.DB_PATH || './sqlite.db' }
// })
// using mysql
const knex = require('knex')({
client: 'mysql2',
connection: {
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'yourusername',
password: process.env.DB_PASSWORD || 'yourpassword',
database: process.env.DB_DATABASE || 'yourdatabasename'
}
})
app.get('/users', async (req, res) => {
res.send({
message: 'List of all users',
users: await knex.select().from('users')
})
})
app.get('/users/:id', async (req, res) => {
res.send({
message: 'List of all users',
user: await knex
.select()
.from('users')
.where('id', Number(req.params.id))
})
})
app.listen(port, () => {
console.log(`Express app is listening on localhost:${port}`)
})