tiny-https-server

Easy tiny https web server middleware.

Usage no npm install needed!

<script type="module">
  import tinyHttpsServer from 'https://cdn.skypack.dev/tiny-https-server';
</script>

README

tiny-https-server: Easy tiny https web server middleware.

npm-version npm-week-downloads

Easy tiny https web server middleware. Easy to use and configure. Handle subdomains. Serving static files.

Installing

npm install tiny-https-server

Usage example

app.js

require('log-report');

if (require("try-to-run")()) {
    // isMainThread
    return;
}

console.time("Time");
var options = require("config-sets").tiny_https_server;
options.subdomains = { test: "./public/test" };

var server = require("tiny-https-server");
//var server = require("./server.min.js");

// Console log
server.on("request", function (req, res, next) {
    console.timeLog("Time", `Url: ${req.headers.host}${req.url}`);
    next();
});
// Test page.
server.on("request", function (req, res, next) {
    if (req.url === "/test") {
        res.writeHead(200, { "Content-Type": "text/plain" });
        res.write("Test page.");
        res.end();
    }
    else
        next();
});
// Simulated crash ...
server.on("request", function (req, res, next) {
    if (req.url === "/crash") {
        throw new Error("Simulated crash ...");
    }
    else
        next();
});


var port = options.port === 443 ? "" : ":" + options.port;
var urls = [
    `http://localhost${port}/`,
    `http://test.localhost${port}/`,
    `http://localhost${port}/test`,
    `http://test.localhost${port}/test`,
    `http://localhost${port}/home`
];

// Opens the URLs in the default browser.
while (urls.length) {
    require("browse-url")(urls.shift());
}

Config-sets file

config-sets.json

{
  "production": {
    "tiny_https_server": {
      "domain": "localhost",
      "port": 443,
      "logDir": "./log/tiny-https-server",
      "document_root": "./public/www",
      "directory_index": "index.html",
      "pathToError_404": "./error_404.html",
      "pathToPrivkey": "./cert/localhost-key.pem",
      "pathToCert": "./cert/localhost-cert.pem",
      "subdomains": {
        "test": "./public/test"
      }
    },
    "log_report": {
      "logDir": "./log/log-report",
      "enabled": true,
      "clear_on_startup": false,
      "save_only_uncaughtException": true
    },
    "browse_url": {
      "launch_url": "",
      "enabled": false
    }
  },
  "development": {
    "tiny_https_server": {
      "subdomains": {
        "test": "./public/test"
      }
    },
    "log_report": {
      "logDir": "./log/log-report",
      "enabled": true,
      "clear_on_startup": true,
      "save_only_uncaughtException": false
    },
    "browse_url": {
      "launch_url": "http://localhost:1337/",
      "enabled": true
    }
  }
}

License

MIT

Copyright (c) 2021 Manuel Lõhmus manuel@hauss.ee