-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreqhandler.js
37 lines (35 loc) · 1 KB
/
reqhandler.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
const express = require("express");
const aws_sdk = require("aws-sdk");
require("dotenv").config();
const s3 = new aws_sdk.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
});
const app = express();
app.get("/*", async (req, res) => {
const host = req.hostname;
const id = host.split(".")[0];
console.log(`ID Accessed: ${id}`);
const filePath = req.path;
console.log("File: ", filePath);
const contents = await s3
.getObject({
Bucket: "webdeploy-beta",
Key: `output/dirs/${id}${filePath}`,
})
.promise();
const type = filePath.endsWith("html")
? "text/html"
: filePath.endsWith("css")
? "text/css"
: filePath.endsWith("js")
? "application/javascript"
: filePath.endsWith("png")
? "image/png"
: filePath.endsWith("jpg") || filePath.endsWith("jpeg")
? "image/jpeg"
: "text/plain";
res.set("Content-Type", type);
res.send(contents.Body);
});
app.listen(3001);