|
2 | 2 |
|
3 | 3 | const express = require('express') |
4 | 4 | const netApi = require('net-browserify') |
5 | | -const request = require('request') |
6 | 5 | const compression = require('compression') |
7 | 6 | const path = require('path') |
8 | 7 |
|
9 | 8 | // Create our app |
10 | 9 | const app = express() |
11 | 10 |
|
12 | | -app.use(function (req, res, next) { |
13 | | - res.header('Access-Control-Allow-Origin', req.get('Origin') || '*') |
14 | | - res.header('Access-Control-Allow-Credentials', 'true') |
15 | | - res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE') |
16 | | - res.header('Access-Control-Expose-Headers', 'Content-Length') |
17 | | - res.header( |
18 | | - 'Access-Control-Allow-Headers', |
19 | | - 'Accept, Authorization, Content-Type, X-Requested-With, Range' |
20 | | - ) |
21 | | - if (req.method === 'OPTIONS') { |
22 | | - return res.send(200) |
23 | | - } else { |
24 | | - return next() |
25 | | - } |
26 | | -}) |
27 | | - |
28 | 11 | app.use(compression()) |
29 | | -app.use(netApi()) |
| 12 | +app.use(netApi({ allowOrigin: '*' })) |
30 | 13 | app.use(express.static(path.join(__dirname, './public'))) |
31 | | - |
32 | 14 | app.use(express.json({ limit: '100kb' })) |
33 | 15 |
|
34 | | -app.all('*', function (req, res, next) { |
35 | | - // Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment |
36 | | - res.header('Access-Control-Allow-Origin', '*') |
37 | | - res.header('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE') |
38 | | - res.header( |
39 | | - 'Access-Control-Allow-Headers', |
40 | | - req.header('access-control-request-headers') |
41 | | - ) |
42 | | - |
43 | | - if (req.method === 'OPTIONS') { |
44 | | - // CORS Preflight |
45 | | - res.send() |
46 | | - } else { |
47 | | - const targetURL = req.header('Target-URL') |
48 | | - if (!targetURL) { |
49 | | - res.status(404).send({ error: '404 Not Found' }) |
50 | | - return |
51 | | - } |
52 | | - const newHeaders = req.headers |
53 | | - newHeaders.host = targetURL |
54 | | - .replace('https://', '') |
55 | | - .replace('http://', '') |
56 | | - .split('/')[0] |
57 | | - request( |
58 | | - { |
59 | | - url: targetURL + req.url, |
60 | | - method: req.method, |
61 | | - json: req.body, |
62 | | - headers: req.headers |
63 | | - }, |
64 | | - function (error, response, body) { |
65 | | - if (error) { |
66 | | - console.error(error) |
67 | | - console.error('error: ' + response.statusCode) |
68 | | - } |
69 | | - // console.log(body); |
70 | | - } |
71 | | - ).pipe(res) |
72 | | - } |
73 | | -}) |
74 | | - |
75 | 16 | // Start the server |
76 | 17 | const server = app.listen(process.argv[2] === undefined ? 8080 : process.argv[2], function () { |
77 | 18 | console.log('Server listening on port ' + server.address().port) |
|
0 commit comments