-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (102 loc) · 3.22 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
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
// Load the env variables
require('dotenv').config()
// Set up the express app
const express = require('express')
const app = express()
// For parsing application/json
app.use(express.json())
// CommonJS node-fetch
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args))
/**
* A function that hits the SpeedVitals API
* @param path the path on the baseURL to hit
* @param baseURL the origin of the paths to be hit
* @returns void
*/
const speedTestTTFB = async (path, baseURL) => {
if (path && baseURL && path.length > 0 && baseURL.length > 0) {
let regions = ['asia', 'america', 'europe']
for (const region of regions) {
console.log(`[Boosting] URL: ${baseURL}${path}, Region: ${region}`)
const resp = await fetch('https://api.speedvitals.com/v1/ttfb-tests', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': process.env.SPEED_VITALS_KEY,
},
body: JSON.stringify({ url: `${baseURL}${path}`, region }),
})
if (resp.ok) {
const data = await resp.json()
console.log('Status:', resp.status)
}
}
}
}
/**
* A function that validates paths array passed in the post body
* @param paths the list of paths to be hit
* @example [ { path: "/home" }, { path: "/about" } ]
* @returns Boolean
*/
const checkPaths = (paths) => {
if (paths) {
try {
for (const p of paths) {
if (p.path) {
} else {
return false
}
}
return true
} catch (e) {
console.log('Error while checking paths:')
console.log(e.message || e.toString())
return false
}
}
return false
}
app.post('/', async (req, res) => {
if (req.method !== 'POST') {
res.status(200).json({ code: 0, message: 'Method Not Allowed.' })
return
}
// Check if some body is passed
if (req.body) {
// Check if SPEED_VITALS_KEY exists
if (req.body.SPEED_VITALS_KEY) {
// Check if SPEED_VITALS_KEY matches
if (req.body.SPEED_VITALS_KEY === process.env.SPEED_VITALS_KEY) {
// Check if baseURL exists
if (req.body.baseURL && req.body.baseURL.length > 0) {
// Check if paths are passed as expected
if (checkPaths(req.body.paths)) {
// All looks ok, pass code: 1 and run the optimizer
res.status(200).json({ code: 1 })
for (const url of req.body.paths) {
await speedTestTTFB(url.path, req.body.baseURL)
}
} else {
res.status(200).json({ code: 0, message: 'paths does not match the expected format.' })
return
}
} else {
res.status(200).json({ code: 0, message: 'baseURL is required.' })
return
}
} else {
res.status(200).json({ code: 0, message: 'SPEED_VITALS_KEY is incorrect.' })
return
}
} else {
res.status(200).json({ code: 0, message: 'SPEED_VITALS_KEY is required.' })
return
}
} else {
res.status(200).json({ code: 0, message: 'SPEED_VITALS_KEY, paths and baseURL is required.' })
return
}
})
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on http://localhost:${port}`))