This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.53 KB
/
index.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
57
58
59
60
61
62
63
const express = require('express');
const proxy = require('http-proxy-middleware');
var app = express();
const port = process.env.PORT || 3000;
// Add headers
app.use((req, res, next) => {
const allowedOrigins = [
'http://127.0.0.1:3000',
'http://localhost:3000',
'http://127.0.0.1:8080',
'http://localhost:8080',
'https://soundnode-redux.herokuapp.com',
'https://redux-music.herokuapp.com',
];
const origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// proxy middleware options
const optionsV1 = {
target: 'https://api.soundcloud.com',
changeOrigin: true,
ws: true,
pathRewrite: {
'^/sc/v1': '/',
},
};
const optionsV2 = {
target: 'https://api-v2.soundcloud.com', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
'^/sc/v2': '/',
},
};
const proxyV1 = proxy(optionsV1);
const proxyV2 = proxy(optionsV2);
app.use('/sc/v1', proxyV1);
app.use('/sc/v2', proxyV2);
// function logger(req, res, next) {
// console.log(new Date(), req.method, req.url);
// next();
// }
// app.use(logger);
app.listen(port, () => {
console.log('Server running at port:', port);
});