diff --git a/Dockerfile b/Dockerfile index 3f6abff..1b6bcdf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,6 @@ WORKDIR /app ADD package.json . RUN npm install -ADD index.js . +ADD index.js index.html ./ CMD npm start diff --git a/README.md b/README.md index a3ed5fa..7d0fd6b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -# CorsProxy -Super simple CORS proxy supporting most (maybe all?) HTTP verbs. +![CorsProxy](logo.png) + +> A proxy enabling CORS headers on responses, supporting most (maybe all?) HTTP verbs. # Usage @@ -7,6 +8,6 @@ Super simple CORS proxy supporting most (maybe all?) HTTP verbs. 1. `node index.js` 1. Navigate to `http://localhost:8000/http://www.example.com` -To change the listening port either set the `PORT` environment variable to pass it in as an argument. +To change the listening port either set the `PORT` environment variable to pass it in as an argument. E.g. `node index.js --port=8080` diff --git a/index.html b/index.html new file mode 100644 index 0000000..4de3f20 --- /dev/null +++ b/index.html @@ -0,0 +1,95 @@ + + + + + + + CorsProxy + + + + + + + + + + + + + + + + + + + +
+

+ CorsProxy + +

+ +
+ A proxy enabling CORS headers on responses, supporting most (maybe all?) HTTP verbs. +
+ +

+ Enter the URL you would like to proxy to and press the "Proxy" button +

+ + +
+
+
+ + + +
+
+
+
+
+
+ +
+ +
+ Source code available on GitHub, GitLab, Bitbucket & my own Gitea server. +
+
+ +
+
+
+ +
+
+
+ + + + diff --git a/index.js b/index.js index 5eeb9a9..3a54a6f 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const request = require('request'); const http = require('http'); const url = require('url'); +const fs = require('fs').promises; let PORT = process.env.PORT || 8000; process.argv.forEach(function (arg) { @@ -11,6 +12,10 @@ process.argv.forEach(function (arg) { }); http.createServer((req, response) => { + if (!req.url.substring(1)) { + return loadWebpage(req, response); + } + let remoteURL = url.parse(req.url.substring(1)); if(!remoteURL.hostname || remoteURL.hostname === 'localhost') return response.end(); @@ -58,3 +63,17 @@ http.createServer((req, response) => { .on('end', () => response.end()); }).listen(PORT); + +function loadWebpage(req, res) { + fs.readFile(__dirname + "/index.html") + .then(contents => { + res.setHeader("Content-Type", "text/html"); + res.writeHead(200); + res.end(contents); + }) + .catch(err => { + res.writeHead(500); + res.end(err); + return; + }); +}