UNPKG

node-http-server

Version:

A very simple and fast http server for node, bash, and spawnable from C, Python etc. It is lightweight and great for embedded solutions as well as everyday development or public facing apps.

193 lines (148 loc) 14.5 kB
<!DOCTYPE html> <html> <head> <title>https-and-http-google-proxy</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="../../assets/file_1.css"/> <link rel="stylesheet" media="all" href="../../assets/file_5.css"/> <link rel="stylesheet" media="all" href="../../assets/32px.png"/> <link rel="stylesheet" media="all" href="../../assets/40px.png"/> <link rel="stylesheet" media="all" href="../../assets/throbber.gif"/> <link rel="stylesheet" media="all" href="../../assets/file_7.css"/> <link rel="stylesheet" media="all" href="../../assets/file_8.css"/> <script src="../../assets/file_2.js" type="text/javascript"></script> <script src="../../assets/file_3.js" type="text/javascript"></script> <script src="../../assets/file_4.js" type="text/javascript"></script> <script src="../../assets/file_6.js" type="text/javascript"></script> <script type="application/json" id="jsTreeJSON"> [{"id":"./example/basic/https-advancedApp.js","text":"https-advancedApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-advancedApp.js.html"},"state":{"selected":false}},{"id":".","text":".","icon":"jstree-folder","parent":"#","a_attr":{"href":"../.."},"state":{"opened":true}},{"id":"./example","text":"example","icon":"jstree-folder","parent":".","a_attr":{"href":".."},"state":{"opened":true}},{"id":"./example/readme.md","text":"readme.md","icon":"jstree-file","parent":"./example","a_attr":{"href":"../readme.md.html"},"state":{"selected":false}},{"id":"./example/advanced","text":"advanced","icon":"jstree-folder","parent":"./example","a_attr":{"href":"../advanced"},"state":{"opened":false}},{"id":"./example/advanced/basicTemplate.js","text":"basicTemplate.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/basicTemplate.js.html"},"state":{"selected":false}},{"id":"./example/advanced/https-ONLY-basicTemplate.js","text":"https-ONLY-basicTemplate.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/https-ONLY-basicTemplate.js.html"},"state":{"selected":false}},{"id":"./example/advanced/https-basicTemplate.js","text":"https-basicTemplate.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/https-basicTemplate.js.html"},"state":{"selected":false}},{"id":"./example/advanced/https-randomCSS.js","text":"https-randomCSS.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/https-randomCSS.js.html"},"state":{"selected":false}},{"id":"./example/advanced/https-requestBenchmark.js","text":"https-requestBenchmark.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/https-requestBenchmark.js.html"},"state":{"selected":false}},{"id":"./example/advanced/randomCSS.js","text":"randomCSS.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/randomCSS.js.html"},"state":{"selected":false}},{"id":"./example/advanced/requestBenchmark.js","text":"requestBenchmark.js","icon":"jstree-file","parent":"./example/advanced","a_attr":{"href":"../advanced/requestBenchmark.js.html"},"state":{"selected":false}},{"id":"./example/basic","text":"basic","icon":"jstree-folder","parent":"./example","a_attr":{"href":"../basic"},"state":{"opened":false}},{"id":"./example/basic/advancedApp.js","text":"advancedApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/advancedApp.js.html"},"state":{"selected":false}},{"id":"./example/basic/basicApp.js","text":"basicApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/basicApp.js.html"},"state":{"selected":false}},{"id":"./example/basic/basicLogging.js","text":"basicLogging.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/basicLogging.js.html"},"state":{"selected":false}},{"id":"./example/basic/cluster-basicApp.js","text":"cluster-basicApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/cluster-basicApp.js.html"},"state":{"selected":false}},{"id":"./example/basic/https-ONLY-basicApp.js","text":"https-ONLY-basicApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-ONLY-basicApp.js.html"},"state":{"selected":false}},{"id":"./README.md","text":"README.md","icon":"jstree-file","parent":".","a_attr":{"href":"../../README.md.html"},"state":{"selected":false}},{"id":"./example/basic/https-basicApp.js","text":"https-basicApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-basicApp.js.html"},"state":{"selected":false}},{"id":"./example/basic/https-basicLogging.js","text":"https-basicLogging.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-basicLogging.js.html"},"state":{"selected":false}},{"id":"./example/basic/https-multipleDomains.js","text":"https-multipleDomains.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-multipleDomains.js.html"},"state":{"selected":false}},{"id":"./example/basic/https-verboseApp.js","text":"https-verboseApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/https-verboseApp.js.html"},"state":{"selected":false}},{"id":"./example/basic/multipleDomains.js","text":"multipleDomains.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/multipleDomains.js.html"},"state":{"selected":false}},{"id":"./example/basic/verboseApp.js","text":"verboseApp.js","icon":"jstree-file","parent":"./example/basic","a_attr":{"href":"../basic/verboseApp.js.html"},"state":{"selected":false}},{"id":"./example/proxy","text":"proxy","icon":"jstree-folder","parent":"./example","a_attr":{"href":"."},"state":{"opened":true}},{"id":"./example/proxy/basic.js","text":"basic.js","icon":"jstree-file","parent":"./example/proxy","a_attr":{"href":"basic.js.html"},"state":{"selected":false}},{"id":"./example/proxy/https-and-http-basic.js","text":"https-and-http-basic.js","icon":"jstree-file","parent":"./example/proxy","a_attr":{"href":"https-and-http-basic.js.html"},"state":{"selected":false}},{"id":"./example/proxy/https-and-http-google-proxy.js","text":"https-and-http-google-proxy.js","icon":"jstree-file","parent":"./example/proxy","a_attr":{"href":"https-and-http-google-proxy.js.html"},"state":{"selected":true}},{"id":"./server","text":"server","icon":"jstree-folder","parent":".","a_attr":{"href":"../../server"},"state":{"opened":false}},{"id":"./server/Config.js","text":"Config.js","icon":"jstree-file","parent":"./server","a_attr":{"href":"../../server/Config.js.html"},"state":{"selected":false}},{"id":"./server/Server.js","text":"Server.js","icon":"jstree-file","parent":"./server","a_attr":{"href":"../../server/Server.js.html"},"state":{"selected":false}},{"id":"./bin","text":"bin","icon":"jstree-folder","parent":".","a_attr":{"href":"../../bin"},"state":{"opened":false}},{"id":"./bin/nhs.js","text":"nhs.js","icon":"jstree-file","parent":"./bin","a_attr":{"href":"../../bin/nhs.js.html"},"state":{"selected":false}},{"id":"./local-certs","text":"local-certs","icon":"jstree-folder","parent":".","a_attr":{"href":"../../local-certs"},"state":{"opened":false}},{"id":"./local-certs/readme.md","text":"readme.md","icon":"jstree-file","parent":"./local-certs","a_attr":{"href":"../../local-certs/readme.md.html"},"state":{"selected":false}}] </script> </head> <body class="markdown-body"> <h1> https-and-http-google-proxy </h1> <div class="subHeading"> ./example/proxy/https-and-http-google-proxy.js </div> <button type="button" class="navigatorToggle">&#9776; files...</button> <nav class="navigationTree minimized"> <input type="text" id="jsTreeSearch" placeholder="search..."/> <div></div> </nav> <section> <ul class="sectionDetails "> <li id="section-1"> <div class="annotation"> </div><div class="content"> <div class="highlight"><pre><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>( <span class="hljs-string">'util'</span> );</pre></div> </div> </li> <li id="section-2"> <div class="annotation"> <p>import the <code>node-http-server</code> module <code>const server=require(‘node-http-server’);</code></p> </div><div class="content"> <div class="highlight"><pre><span class="hljs-keyword">const</span> server=<span class="hljs-built_in">require</span>(<span class="hljs-string">'../../server/Server.js'</span>);</pre></div> </div> </li> <li id="section-3"> <div class="annotation"> <p>I am using request for simplicty sake here, you can too.</p> </div><div class="content"> <div class="highlight"><pre><span class="hljs-keyword">const</span> proxy=<span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>); <span class="hljs-keyword">const</span> config=<span class="hljs-keyword">new</span> server.Config; config.verbose=<span class="hljs-literal">true</span>; config.port=<span class="hljs-number">8000</span>; config.https.privateKey = <span class="hljs-string">`<span class="hljs-subst">${__dirname}</span>/../../local-certs/private/server.key`</span>; config.https.certificate= <span class="hljs-string">`<span class="hljs-subst">${__dirname}</span>/../../local-certs/client.crt`</span>; config.https.ca= <span class="hljs-string">`<span class="hljs-subst">${__dirname}</span>/../../local-certs/private/rootCA.pem`</span>; config.https.port = <span class="hljs-number">4433</span>;</pre></div> </div> </li> <li id="section-4"> <div class="annotation"> <p>lets ignore ssl issues and make a giant security hole since we are proxying https too… be careful when proxying ssl. you should actually set your ca and certs properly. this is just an example</p> </div><div class="content"> <div class="highlight"><pre>process.env.NODE_TLS_REJECT_UNAUTHORIZED = <span class="hljs-number">0</span>; <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">gotRequest</span>(<span class="hljs-params">request,response,serve</span>)</span>{</pre></div> </div> </li> <li id="section-5"> <div class="annotation"> <p>google proxy! handle things fast</p> </div><div class="content"> <div class="highlight"><pre> <span class="hljs-keyword">let</span> encoding=<span class="hljs-string">'binary'</span>; proxy( { <span class="hljs-attr">url</span>: <span class="hljs-string">`<span class="hljs-subst">${request.uri.protocol}</span>://www.google.com<span class="hljs-subst">${request.uri.path}</span><span class="hljs-subst">${request.uri.search}</span>`</span>, <span class="hljs-attr">encoding</span>:encoding }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error, proxiedResponse, proxiedBody</span>) </span>{ <span class="hljs-keyword">if</span> (error) { request.statusCode=<span class="hljs-number">500</span>; serve(request,response,<span class="hljs-built_in">JSON</span>.stringify(error)); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">if</span>(!proxiedResponse.headers[<span class="hljs-string">'content-type'</span>]&amp;&amp;proxiedResponse.headers[<span class="hljs-string">'Content-Type'</span>]){ proxiedResponse.headers[<span class="hljs-string">'content-type'</span>]=proxiedResponse.headers[<span class="hljs-string">'Content-Type'</span>]; } <span class="hljs-keyword">if</span>(!proxiedResponse.headers[<span class="hljs-string">'content-type'</span>]){ proxiedResponse.headers[<span class="hljs-string">'content-type'</span>]=<span class="hljs-string">''</span>; } <span class="hljs-keyword">if</span>(proxiedResponse.headers[<span class="hljs-string">'content-type'</span>].indexOf(<span class="hljs-string">'text/html'</span>)&gt;<span class="hljs-number">-1</span>){ <span class="hljs-keyword">const</span> position=proxiedBody.match(<span class="hljs-regexp">/&lt;body([^&gt;]*)&gt;/i</span>); <span class="hljs-keyword">if</span>(position&amp;&amp;position.index){ proxiedBody=proxiedBody.slice(<span class="hljs-number">0</span>, position.index+position[<span class="hljs-number">0</span>].length) + <span class="hljs-string">` &lt;style&gt; .proxyBanner{ height:5em; background:rgb(200,220,240); font-size:2em; line-height:5em; box-shadow:0 0 .5em rgba(0,0,0,.7); text-align:center; } &lt;/style&gt; &lt;section class='proxyBanner'&gt;Welcome to Google!&lt;/section&gt; `</span>+ proxiedBody.slice(position.index+position[<span class="hljs-number">0</span>].length) } } response.headers=proxiedResponse.headers; serve(request,response,proxiedBody); <span class="hljs-keyword">return</span>; } ); <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } server.deploy(config); server.onRawRequest=gotRequest;</pre></div> </div> </li> </ul> </section> </body> </html>