-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (31 loc) · 1.09 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
const express = require('express');
const { getSignedUrl } = require('@aws-sdk/cloudfront-signer');
const app = express();
const dotenv=require('dotenv')
const bodyParser = require('body-parser');
dotenv.config()
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index', { signedUrl: null }); // Initial rendering without a signed URL
});
app.post('/', (req, res) => {
const selectedVideo = req.body.video
console.log('Selected video:', selectedVideo);
try {
const signedUrl = getSignedUrl({
keyPairId: process.env.CLOUDFRONT_KEYPAIR_ID,
privateKey: process.env.CLOUDFRONT_PRIVATE_KEY,
url: process.env.DISTRIBUTION_LINK+'/'+selectedVideo,
dateLessThan: new Date(Date.now() + 1000 * 60),
});
res.render('index', { signedUrl });
} catch (error) {
console.error('Error generating signed URL:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});