-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (40 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
const express = require('express');
const axios = require('axios');
const NodeCache = require('node-cache');
class CachingProxyServer {
constructor(port, origin){
this.port = port;
this.origin = origin;
this.cache = new NodeCache({stdTTL:3600});
this.app = express();
}
async handleRequest(req, res){
const url = `${this.origin.replace(/\/+$/, '')}/${req.originalUrl.replace(/^\/+/, '')}`;
console.log(`Forwarding request to: ${url}`); // Log the forwarded URL
const cachedResponse = this.cache.get(url);
if(cachedResponse){
res.setHeader('X-Cache', 'HIT');
return res.status(200).send(cachedResponse.data);
}
try {
const response = await axios.get(url);
const responseData = response.data; // Simplified data
this.cache.set(url, responseData);
res.setHeader('X-Cache', 'MISS');
res.status(response.status).send(responseData);
} catch (error) {
console.error(`Request failed with status code: ${error.response ? error.response.status : 500}`);
res.status(error.response ? error.response.status : 500).send(error.message);
}
}
start(){
this.app.get('*', this.handleRequest.bind(this));
this.app.listen(this.port, () => {
console.log(`Caching Proxy Server running on port ${this.port}`);
});
}
clearCache() {
this.cache.flushAll();
}
}
module.exports = CachingProxyServer;