-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·45 lines (42 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
executable file
·45 lines (42 loc) · 1.3 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
const express = require('express');
const app = express();
const request = require('request-promise');
app.use(express.static('src'));
function isResourceRequest(url) {
const ends = ['html', 'jpeg', 'png', 'js', 'map'];
for(let i = 0; i < ends.length;i++) {
if(url.indexOf(`.{ends[i]}`) !== -1) {
return true;
}
}
return false;
}
app.use(function (req, res, next) {
res.set({
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-type, Authorization, X-Access-Token',
'Access-Control-Allow-Credentials': true
});
if ('OPTIONS' === req.method) {
res.sendStatus(200);
} else {
next();
}
});
app.get('/api/movies', (req,res) => {
request({
uri: 'http://s3.amazonaws.com/vodassets/showcase.json',
}).then((body) => {
res.status(200).send(body).end();
}).catch((err) => res.status(500).send(err))
});
app.get('/*', (req, res, next) => {
if(!isResourceRequest(req.url)) {
res.sendFile(`${__dirname}/src/index.html`);
return;
}
next();
});
const port = process.env.PORT || 8000;
app.listen(port, () => console.log(`Go to this link http://localhost:${port}`));