-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (40 loc) · 1.14 KB
/
index.js
File metadata and controls
50 lines (40 loc) · 1.14 KB
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
const express = require('express')
const dotenv = require('dotenv')
const path = require('path')
const fs = require('fs')
dotenv.config()
const aws = require('aws-sdk')
const s3 = new aws.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})
const app = express()
app.post('/distributed', (req, res, next) => {
const params = {
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: 'index.html'
}
s3.getObject(params, (error, data) => {
if (error) {
res.status(500).json({
message: 'Failed at pulling from S3 Bucket'
})
}
fs.writeFileSync(path.resolve(__dirname, 'static/index.html'),
data.Body.toString())
})
res.status(200).json({
message: 'Successfully pulled from S3 Bucket'
})
})
app.get('*', (req, res, next) => {
try {
const indexFile = fs.readFileSync(path.resolve(__dirname, 'static/index.html'))
res.send(indexFile.toString())
} catch (error) {
res.status(500).send('Pulling from S3 Bucket is required before distribute ECHO-FRONT')
}
})
app.listen(process.env.SERVER_PORT, () => {
console.log('Server is running...')
})