|
const http = require("http"); |
|
const https = require("https"); |
|
const PORT = process.env.PORT || 4000; |
|
|
|
class APIError extends Error { |
|
constructor(message, code = 400) { |
|
super(message); |
|
this.code = code; |
|
} |
|
} |
|
|
|
http.createServer(async (req, res) => { |
|
|
|
const buffers = []; |
|
|
|
for await (const chunk of req) { |
|
buffers.push(chunk); |
|
} |
|
|
|
const data = Buffer.concat(buffers).toString(); |
|
if (data) { |
|
req.body = JSON.parse(data); |
|
} |
|
try { |
|
if (req.url === "/licipoapi/proxy") { |
|
|
|
if (req.method === "OPTIONS") { |
|
res.writeHead(200, { |
|
"Access-Control-Allow-Origin": "*", |
|
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", |
|
"Access-Control-Allow-Headers": "Content-Type, Authorization, apikey", |
|
"Access-Control-Max-Age": "3600" |
|
}); |
|
res.end(); |
|
return; |
|
} |
|
|
|
const authorizationHeader = req.headers.authorization; |
|
const apiKey = req.headers.apikey; |
|
if (!authorizationHeader) { |
|
throw new APIError("Unauthorized", 401); |
|
} |
|
const {url: endPoint, data: body} = req.body; |
|
const protocol = endPoint.startsWith("https") ? https : http; |
|
console.log(`Request method : ${req.method} to ${endPoint}`); |
|
const proxyReq = protocol.request(endPoint, { |
|
headers: { |
|
authorization: authorizationHeader, |
|
apikey: apiKey, |
|
"Content-Type": "application/json", |
|
"Content-Length": Buffer.byteLength(JSON.stringify(body)) |
|
}, |
|
method: "GET", |
|
}, (proxyRes) => { |
|
const _buffers = []; |
|
|
|
proxyRes.on("data", (chunk) => { |
|
_buffers.push(chunk); |
|
}); |
|
|
|
proxyRes.on("end", () => { |
|
const _data = Buffer.concat(_buffers).toString(); |
|
console.log(_data); |
|
if (+proxyRes.statusCode === 200) { |
|
const parsedData = JSON.parse(_data); |
|
const _modifiedResponse = JSON.stringify({ data: parsedData}); |
|
const {"content-length": contentLength, ...others} = proxyRes.headers; |
|
res.writeHead(proxyRes.statusCode, {'Access-Control-Allow-Origin': "*", ...others, 'Content-Length': Buffer.byteLength(_modifiedResponse)}); |
|
res.write(_modifiedResponse); |
|
} else { |
|
res.writeHead(proxyRes.statusCode, {'Access-Control-Allow-Origin': "*", ...proxyRes.headers}); |
|
res.write(_data); |
|
} |
|
res.end(); |
|
}); |
|
}); |
|
proxyReq.on("error", (err) => { |
|
console.error(err); |
|
res.writeHead(500); |
|
res.write(err.message); |
|
res.end(); |
|
}); |
|
proxyReq.write(JSON.stringify(body)); |
|
proxyReq.end(); |
|
|
|
} |
|
} catch (error) { |
|
if (error instanceof APIError) { |
|
res.writeHead(error.code); |
|
res.write(error.message); |
|
} else { |
|
res.writeHead(400); |
|
res.write(error.message); |
|
} |
|
res.end(); |
|
} |
|
}).listen(PORT); |