-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
42 lines (36 loc) · 846 Bytes
/
api.js
File metadata and controls
42 lines (36 loc) · 846 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
import express from 'express'
import cors from 'cors'
import mariadb from 'mariadb'
// customise for you
// e.g. yours will be host: 'localhost'
const DB = {
host: 'mariadb',
user: 'api',
port: '3306',
password: 'tiger',
database: 'project',
}
const app = express()
const port = 8080
app.use(cors({origin: 'null'}))
const pool = mariadb.createPool({
...DB,
connectionLimit: 5,
});
app.get('/people', async (req, res) => {
console.log(req.headers)
let conn
try {
conn = await pool.getConnection()
const rows = await conn.query('select * from people')
res.send(rows)
}
catch(e) {
console.log(e)
res.status(500).send('An error occured');
}
finally {
conn.release()
}
})
app.listen(port, () => console.log(`Listening on port ${port}`))