-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
48 lines (40 loc) · 1.47 KB
/
server.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
const express = require('express')
const bodyParser = require('body-parser')
const helmet = require('helmet')
const boom = require('boom')
const { Client: PostgresClient } = require('pg')
const { parse: parseConnectionString } = require('pg-connection-string')
const homeRoute = require('./routes/home')
const faqRoute = require('./routes/faq')
const createLogRoute = require('./routes/create-log')
const logRoute = require('./routes/log')
const editLogRoute = require('./routes/edit-log')
const createEntryRoute = require('./routes/create-entry')
const DEFAULT_CONNECTION_STRING = 'postgres://localhost/dicelogger'
const DEFAULT_PORT = 3000
;(async function () {
const app = express()
const connectionString = parseConnectionString(process.env.DATABASE_URL || DEFAULT_CONNECTION_STRING)
const pg = new PostgresClient(connectionString)
const port = process.env.PORT || DEFAULT_PORT
await pg.connect()
app.use(helmet())
app.use(express.static('static'))
app.use(bodyParser.urlencoded({ extended: false }))
homeRoute(app, pg)
faqRoute(app, pg)
createLogRoute(app, pg)
logRoute(app, pg)
editLogRoute(app, pg)
createEntryRoute(app, pg)
app.use((err, req, res, next) => {
if (boom.isBoom(err)) {
const { payload } = err.output
res.status(payload.statusCode).send(payload.error)
} else {
console.error(err.stack)
res.status(500).send('Server Error')
}
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
})()