forked from clearlyTHUYDOAN/mux-direct-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (48 loc) · 1.46 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const axios = require('axios')
const express = require('express')
const app = express()
// Express configuration
app.use(express.urlencoded({extended: true}));
app.use(express.json())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
// Allow you to store API keys in a .env file and load them in as needed
const dotenv = require('dotenv');
dotenv.config();
const baseUrl = 'https://api.mux.com'
const port = process.env.PORT || 3001
const options = {
headers: {
'User-Agent': `Mux Direct Upload Button`,
'Content-Type': 'application/json',
Accept: 'application/json',
},
auth: {
username: process.env.API_TOKEN_ID,
password: process.env.API_SECRET,
},
mode: 'cors',
}
app.post('/upload', async (req, res) => {
try {
const response = await axios.post(`${baseUrl}/video/v1/uploads`, {
"cors_origin": "*",
"new_asset_settings": {
"playback_policy": [
"signed"
],
} }, options)
return res.send(response.data && response.data.data);
} catch (errorRes) {
return Promise.reject(
(errorRes.response && errorRes.response.data.error) || errorRes
);
}
})
app.listen(port, () => {
console.log(`👂🏻 Example app listening on port ${process.env.PORT}`)
})