UNPKG

tiny-https-server

Version:

Tiny web server with HTTPS support, static file serving, subdomain support, middleware support, and service worker support.

227 lines (217 loc) 10.9 kB
<!DOCTYPE html> <html> <head> <title>Tiny HTTPS Server</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" /> <script crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css" /> <script crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js"></script> <style> #list-headers ul { list-style: none; padding-left: .5em; } #list-headers > ul { padding: 0; } #list-headers h1, h2, h3, h4, h5 { white-space: nowrap; } .markdown-body { padding-left: 2em; } @media (min-width: 992px) { .vh-lg-100{ height: 100vh !important; } } </style> </head> <body> <div class="row w-100"> <div class="col-12 text-center"> <h1 id="tiny-https-server">Tiny HTTPS Server</h1> <p>Tiny web server with HTTPS support, static file serving, subdomain support,<br> middleware support, service worker support, and clastering support.<br> This manual is also available in <a href="https://manuel-lohmus.github.io/tiny-https-server/README.html">HTML5</a>.</p> </div> </div> <div class="row w-100"> <div class="col-lg-3 d-lg-inline"> <div class="sticky-top overflow-auto vh-lg-100"> <div id="list-headers" class="list-group mt-2 ms-lg-2 ms-4"> <h4 id="table-of-contents">Table of contents</h4> <ul> <li><a href="#description"><strong>Description</strong></a></li> <li><a href="#features"><strong>Features</strong></a></li> <li><a href="#installation"><strong>Installation</strong></a></li> <li><a href="#testing"><strong>Testing</strong></a></li> <li><a href="#how-to-use"><strong>How to use</strong></a></li> <li><a href="#lets-encrypt-support"><strong>Let's Encrypt support</strong></a></li> <li><a href="#license"><strong>License</strong></a></li> </ul> </div> </div> </div> <div class="col-lg-9 mt-2"> <div class="ps-4 markdown-body" data-bs-spy="scroll" data-bs-target="#list-headers" data-bs-offset="0" tabindex="0"> <h2 id="description">Description</h2> <p>Tiny web server is disain for SPA (Single Page Application). This project is a tiny web server that serves static files, supports HTTPS, subdomains, middleware, and service workers. This module is part of the <a href="https://www.npmjs.com/package/conextra">'conextra'</a> framework, which is a simple and easy-to-use single-page application (SPA) framework. You have to try it! A different solution than MVC (model–view–controller). When you run the server, it will serve the files in the <code>public</code> directory. You can add your own files to the <code>public</code> directory and access them through the server. The server also supports subdomains, so you can access different files based on the subdomain. For example, if you have a file called <code>index.html</code> in the <code>public</code> directory, you can access it through <code>https://localhost/index.html</code>. If you have a file called <code>index.html</code> in the <code>public/subdomain</code> directory, you can access it through <code>https://subdomain.localhost/index.html</code>. The server also supports middleware, so you can add your own middleware functions to the server. The server also supports service workers. For testing uses a self-signed certificate, so you may see a warning in your browser when you access the server. You can ignore the warning and proceed to the server. Yor can mount the express.js style middleware to the server. Example:</p> <pre><code class="language-javascript">const WebCluster = require('tiny-https-server'); const express = require('express'); const admin = express(); admin.get('/', (req, res) =&gt; { res.send('Admin Homepage'); }); consr cluster = WebCluster({ //isDebug: true, parallelism: 'auto 2' }, function _initServer(server) { // Mount express.js style middleware to the server under /admin path, url: 'http://localhost/admin' or 'http://yourdomain.com/admin' server.addRequest({ path: '/admin/*' }, admin); // Mount express.js style middleware to the server under admin subdomain, url: 'http://admin.localhost' or 'http://admin.yourdomain.com' server.addRequest({ host: 'admin' }, admin); }); </code></pre> <h2 id="features">Features</h2> <ul> <li>HTTPS support</li> <li>HTTP to HTTPS redirection</li> <li>static file serving</li> <li>subdomain support</li> <li>service worker support</li> <li>blacklist support</li> <li>middleware support</li> <li>mounting support express.js style</li> <li>clastering support automatic scaling up and down</li> <li>compression support</li> <li>cache control support</li> <li>path validation support</li> <li>traffic-advice support</li> <li>security.txt support</li> <li>acme-challenge support for letsencrypt</li> <li>test self-signed certificate support</li> <li>configurable in JSON format file <code>config-sets.json</code></li> <li>command line support, example: <code>tiny-https-server --help</code> or <code>tiny-https-server --parallelism='auto 1'</code></li> </ul> <h2 id="installation">Installation</h2> <p>You can install 'tiny-https-server' using this command:</p> <p><code>npm install tiny-https-server</code></p> <p>You can also install 'tiny-https-server' globally using this command:</p> <p><code>npm install -g tiny-https-server</code></p> <h2 id="testing">Testing</h2> <p>You can test 'tiny-https-server' on your system using this command:</p> <p><code>node ./node_modules/tiny-https-server/index.test</code></p> <p>or in the 'tiny-https-server' project directory:</p> <p><code>npm test</code></p> <h2 id="how-to-use">How to use</h2> <p>You can use 'tiny-https-server' in your project like this:</p> <pre><code class="language-javascript">const WebCluster = require('tiny-https-server'); consr cluster = WebCluster({ isDebug: true, // Enable debug mode to see logs in the console parallelism: 'auto 2' // Start 2 workers and scale up and down automatically }, function _initServer(server) { // Add a request handler for the /hello path server.on('request', (req, res, next) =&gt; { if (req.url.startsWith('/hello')) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World'); return; } next(); }); }); </code></pre> <br> <p>You can create a separate initServer.js file and use it like this:</p> <pre><code class="language-javascript">const WebCluster = require('tiny-https-server'); const initServer = require('./initServer.js'); const cluster = WebCluster({ isDebug: true, // Enable debug mode to see logs in the console parallelism: 'auto 2' // Start 2 workers and scale up and down automatically }, initServer); </code></pre> <br> <p>in <code>initServer.js</code> file:</p> <pre><code class="language-javascript">module.exports = function _initServer(server) { // Add your own code here // This function is called when the server is initialized // For example, you can add request handlers, middleware, etc. const express = require('express'); const admin = express(); admin.get('/', (req, res) =&gt; { res.send('Admin Homepage'); }); // Mount express.js style middleware to the server under /admin path, url: 'http://localhost/admin' or 'http://yourdomain.com/admin' server.addRequest({ path: '/admin/*' }, admin); // Mount express.js style middleware to the server under admin subdomain, url: 'http://admin.localhost' or 'http://admin.yourdomain.com' server.addRequest({ host: 'admin' }, admin); }; </code></pre> <br> <p>If you install 'tiny-https-server' globally, can use command line like this:</p> <pre><code class="language-bash">tiny-https-server --port=8080 --host=localhost --docroot=./public --parallelism=auto 2 --isDebug=true </code></pre> <h2 id="lets-encrypt-support">Let's Encrypt support</h2> <p>'tiny-https-server' supports Let's Encrypt for obtaining free SSL certificates.<br> This allows you to use HTTPS on your server without having to pay for a certificate.<br> How to add Let's Encrypt support using Certbot &gt; webroot method:</p> <ul> <li>See <a href="https://certbot.eff.org/">Certbot</a> for more information on how to use Certbot to obtain a certificate.</li> <li>See <a href="https://certbot.eff.org/docs/using.html#webroot">Certbot webroot method</a> for more informatsion on how to use the webroot method.</li> <li>Install Certbot on your system.</li> <li>Run the following command to obtain a certificate:<br> <code>certbot certonly --webroot -w /path/to/tiny-https-server/public -d www.yourdomain.com -d yourdomain.com</code> <br> Replace <code>/path/to/tiny-https-server/public</code> with the path to your 'tiny-https-server' public directory and <code>yourdomain.com</code> with your domain name.</li> <li>Certbot saving the certificate to <code>/etc/letsencrypt/live/</code> and renewing it on a regular schedule.</li> <li>In the 'tiny-https-server' configuration file <code>config-sets.json</code>, set the <code>pathToCert</code> and <code>pathToPrivkey</code> options to the path of the certificate and key files: <pre><code class="language-json">{ &quot;production&quot;: { &quot;tiny-https-server&quot;: { &quot;pathToCert&quot;: &quot;/etc/letsencrypt/live/yourdomain.com/fullchain.pem&quot;, &quot;pathToPrivkey&quot;: &quot;/etc/letsencrypt/live/yourdomain.com/privkey.pem&quot; } } } </code></pre> Replace <code>yourdomain.com</code> with your domain name.</li> <li>Run the 'tiny-https-server'.</li> </ul> <h2 id="license">License</h2> <p>This project is licensed under the MIT License.</p> <p>Copyright © Manuel Lõhmus</p> <p><a href="https://www.paypal.com/donate?hosted_button_id=H2ZHLF8U2HGVA"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Donate" /></a></p> <p>Donations are welcome and will go towards further development of this project.</p> <br> <br> <br> </div> </div> </div> <script> (function () { 'use strict'; var isIE = !!document.documentMode; // Detect IE if (!isIE) { // list-group style for headers document.querySelectorAll('#list-headers a') .forEach(function (a) { a.classList.add('list-group-item', 'list-group-item-action') }); } })(); </script> </body> </html>