-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathapp.js
More file actions
81 lines (70 loc) · 2.31 KB
/
app.js
File metadata and controls
81 lines (70 loc) · 2.31 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/*
* Import required packages.
* Packages should be installed with "npm install".
*/
const express = require('express');
const aws = require('aws-sdk');
/*
* Set-up and run the Express app.
*/
const app = express();
app.set('views', './views');
app.use(express.static('./public'));
app.engine('html', require('ejs').renderFile);
app.listen(process.env.PORT || 3000);
/*
* Configure the AWS region of the target bucket.
* Remember to change this to the relevant region.
*/
aws.config.region = 'eu-west-1';
/*
* Load the S3 information from the environment variables.
*/
const S3_BUCKET = process.env.S3_BUCKET;
/*
* Respond to GET requests to /account.
* Upon request, render the 'account.html' web page in views/ directory.
*/
app.get('/account', (req, res) => res.render('account.html'));
/*
* Respond to GET requests to /sign-s3.
* Upon request, return JSON containing the temporarily-signed S3 request and
* the anticipated URL of the image.
*/
app.get('/sign-s3', (req, res) => {
const s3 = new aws.S3();
const fileName = req.query['file-name'];
const fileType = req.query['file-type'];
const s3Params = {
Bucket: S3_BUCKET,
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if(err){
console.log(err);
return res.end();
}
const returnData = {
signedRequest: data,
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
res.write(JSON.stringify(returnData));
res.end();
});
});
/*
* Respond to POST requests to /submit_form.
* This function needs to be completed to handle the information in
* a way that suits your application.
*/
app.post('/save-details', (req, res) => {
// TODO: Read POSTed form data and do something useful
});