-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (59 loc) · 1.86 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
const express = require(`express`)
const morgan = require(`morgan`)
const fsPromises = require(`fs`).promises
const fs = require(`fs`)
const path = require(`path`)
const cors = require(`cors`)
require(`dotenv`).config()
const app = express()
const PORT = process.env.PORT ?? 3000
const TIMEOUT = process.env.TIMEOUT ?? 2000
const SERVER_NAME = process.env.SERVER_NAME
const fetch = require(`node-fetch`)
// Setup logging
app.use(morgan(`dev`))
// To handle body parsing
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const storageDir = path.join(__dirname, `storage`)
if (!fs.existsSync(storageDir)) {
fs.mkdirSync(storageDir)
}
app.use(cors())
async function createFile(req) {
console.log(`${SERVER_NAME}${req.url}`)
const localFilePath = path.join(storageDir, encodeURIComponent(req.url))
const timeout = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Request timed out after ${TIMEOUT} milli-seconds`))
}, TIMEOUT)
})
const response = await Promise.race([
fetch(`${SERVER_NAME}${req.url}`),
timeout
]).catch(() => {
return {ok: false, status: 408}
})
if (!response.ok) {
return response.status
}
const buffer = await response.arrayBuffer()
await fsPromises.writeFile(localFilePath, Buffer.from(buffer))
return false
}
app.use(`/`, async (req, res, next) => {
const localFilePath = path.join(storageDir, encodeURIComponent(req.url))
if (!fs.existsSync(localFilePath)){
const err = await createFile(req)
if (err){
console.warn(`Error ${err} on ${SERVER_NAME}${req.url}`)
res.status(err)
return next(err)
}
}
// Respond with the file
res.sendFile(localFilePath)
})
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`)
})