-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtilia-api.js
116 lines (97 loc) · 3.36 KB
/
tilia-api.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const compression = require('compression')
const rateLimit = require('express-rate-limit')
const express = require('express')
const path = require('path')
const favicon = require('serve-favicon')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const dbtest = require('./database/pgp_db').dbheader;
const fs = require('fs')
const helmet = require('helmet')
const dotenv = require('dotenv')
const util = require('node:util')
const cors = require('cors')
const json5 = require('json5')
const app = express()
dotenv.config()
// create a write stream (in append mode) for the log files.
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'),
{ flags: 'a',
interval: '1d', // rotate daily
path: path.join(__dirname, 'log') })
var logFile = fs.createWriteStream('log.txt', { flags: 'a' })
// Or 'w' to truncate the file every time the process starts.
var logStdout = process.stdout
console.log = function () {
logFile.write(util.format.apply(null, arguments) + '\n')
logStdout.write(util.format.apply(null, arguments) + '\n')
}
// const limiter = rateLimit({
// windowMs: process.env.RATE_WINDOW || 2 * 60 * 1000, // 2 minutes
// max: process.env.MAX_RATE || 10000, // Limit each IP to 100 requests per `window` (here, per 2 minutes)
// standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
// legacyHeaders: false // Disable the `X-RateLimit-*` headers
// })
//app.use(helmet())
//app.disable('x-powered-by')
// Apply the rate limiting middleware to all requests
// app.use(limiter)
app.use(compression())
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use(cors())
app.locals.db = dbtest();
// We get unintentional errors from Tilia when poorly formatted JSON is passed.
// This helps us figure out what's going on:
app.use((req, res, next) => {
express.text({ type: '*/*', 'strict': false, 'inflate': true })(req, res, err => {
try {
if(req._body){
let test = json5.parse(req.body)
req.body = test
}
} catch {
var date = new Date()
if(req._body){
console.log(date.toISOString() + ' {"error": "JSON body will not parse", "body": "' + req.body.replace(/(\r\n|\n|\r)/gm, ' ') + '"}')
return res.status(400)
.json({
success: 0,
status: 'failure',
data: req.body.replace(/(\r\n|\n|\r)/gm, ' '),
message: 'The JSON body will not properly parse.'
})
}
else {
console.log(date.toISOString() + ' {"error": "No JSON body"}')
}
}
next()
})
})
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
var data = require('./routes/data.js')
app.use('/', data)
app.all('*', function (req, res) {
res.redirect('/api')
})
// custom 404
app.use((req, res, next) => {
res.status(404).send("Ain't nobody here but us chickens!")
})
// custom error handler
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
// in production, port is 3001 and server started in script 'www'
// The variable is stored in the gitignored `.env` file.
// This is managed in the www folder.
if (process.env.NODE_ENV === 'development') {
app.listen(3006)
}
module.exports = app