-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
47 lines (42 loc) · 1.42 KB
/
app.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
const express = require('express')
const mysql = require('mysql')
const routes = require('./routes')
const custom_routes = require('./custom_routes')
const app = express()
app.use(express.json())
const pool = mysql.createPool({
connectionLimit: 2,
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
// Support of named placeholders (https://github.com/mysqljs/mysql#custom-format)
queryFormat: function(query, values) {
if (!values) {
return query
}
return query.replace(/\:(\w+)/g, function(matchedSubstring, capturedValue) {
if (values.hasOwnProperty(capturedValue)) {
return this.escape(values[capturedValue])
}
return matchedSubstring
}.bind(this))
},
// Support for conversion from TINYINT(1) to boolean (https://github.com/mysqljs/mysql#custom-type-casting)
typeCast: function(field, next) {
if (field.type === 'TINY' && field.length === 1) {
return field.string() === '1'
}
return next()
}
})
routes.register(app, pool)
custom_routes.register(app, pool)
app.use((error, req, res, next) => {
console.error(error)
res.status(500).json({ "error": "Internal Server Error" })
})
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Listen on ${port}`)
})