-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (46 loc) · 1.53 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
49
// Require the framework and instantiate it
require('dotenv').config()
const fastify = require('fastify')({ logger: false, trustProxy: true })
const autoload = require('fastify-autoload')
const path = require('path')
fastify.register(require('fastify-formbody'))
fastify.register(require('fastify-mysql'), {
connectionString: `mysql://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}/${process.env.DB_DATABASE}`
})
fastify.register(require('fastify-rate-limit'))
fastify.register(autoload, {
dir: path.join(__dirname, 'routes')
})
fastify.register(require('fastify-cors'), {
// put your options here
origin: '*'
})
fastify.register(require('fastify-rate-limit'), {
max: Number.parseInt(process.env.RATELIMIT_Global_Max),
ban: Number.parseInt(process.env.RATELIMIT_Global_Ban),
timeWindow: '1 minute'
})
//custom error handler for fastify-rate-limit
fastify.setErrorHandler(function (error, request, reply) {
if (reply.statusCode === 429) {
error.message = 'You hit the rate limit! Slow down please!'
}
reply.send(error)
})
// Run the server!
const host = '0.0.0.0';
const port = process.env.PORT || 3000;
const start = async () => {
try {
await fastify.listen(port, host)
var datetime = new Date();
console.log("[" + datetime.toISOString().
replace(/T/, ' ').replace(/\..+/, '')
+ " ("+ Intl.DateTimeFormat().resolvedOptions().timeZone
+ ")] — Hello, I'm online and ready to work! My port is " + port)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()