-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (56 loc) · 1.99 KB
/
index.js
File metadata and controls
57 lines (56 loc) · 1.99 KB
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
const express = require('express')
const bodyParser = require('body-parser')
const fs = require('fs')
const https = require('https')
const cryptoConstants = require('crypto').constants
const mainConfig = require('./config/main.config')
const HttpError = require('./lib/HttpError')
const app = express()
app.use(bodyParser.urlencoded({
extended: true,
limit: '5mb'
}))
app.use(bodyParser.json())
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.use('/', express.static('./swaggerui-dist'))
app.use('/pets', require('./routes/pets'))
app.use(function (req, res, next) {
console.warn('Route Not Found: ' + req.url)
res.status(404).json({ code: 404, message: 'Not Found' })
})
app.use(function (err, req, res, next) {
if (err instanceof HttpError) {
// don't expose internal errors because of security
if (err.status >= 400 && err.status < 500) {
console.warn(err.toString())
res.status(err.status).json({ code: err.status, message: err.message })
} else {
console.error(err.toString())
res.status(err.status).json({ code: err.status, message: err.getHttpMessage() })
}
} else {
console.error(err)
res.status(500).json({ code: 500, message: 'Internal Server Error' })
}
})
app.set('etag', false)
if (mainConfig.useHttps) {
const credentials = {
key: fs.readFileSync('/etc/ssl/server.key', 'utf8'),
cert: fs.readFileSync('/etc/ssl/server.crt', 'utf8'),
secureOptions: cryptoConstants.SSL_OP_NO_TLSv1 | cryptoConstants.SSL_OP_NO_TLSv1_1
}
const httpsServer = https.createServer(credentials, app)
httpsServer.listen(mainConfig.port, function () {
console.log(`${mainConfig.serviceName} is listening on https://127.0.0.1:${mainConfig.port}`)
})
} else {
app.listen(mainConfig.port, function () {
console.log(`${mainConfig.serviceName} is listening on http://127.0.0.1:${mainConfig.port}`)
})
}