0.0.1 • Published 4 years ago

robusto v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Robusto

Maintainability Build Status

A Node.js HTTP2 static server.

Installation | Environment setup | API usage | CLI usage | Options | Details | License

Installation

  • Install the stable version:

    npm i robusto
  • Install the latest version:

    npm i stanislavzadiraev/robusto#master

Environment setup

  • Get ready to route requests:

    • Prepare DNS records with the target hostnames for routing requests to the loopback IP address in the local hosts file for a local usage.

    • Prepare DNS records with the target hostnames for routing requests to the current system IP address on the DNS servers for a network usage.

    • Use target IP addresses directly instead of hostnames.

  • Get ready for a high load:

    • Add net.core.somaxconn=65535 to /etc/sysctl.conf.

    • Run sysctl net.core.somaxconn=65535.

API usage

Signature

robusto([options])

Receives

An Object containing the options. Detailed in Options below.

Returns

A Promise resolved with the Http2SecureServer upon success.

Examples

Declare

import robusto from 'robusto'

Execute

  • Create and run a simplex server:

    robusto({
      hostnames: ['localhost']
    })
  • Create and run a complex server:

    robusto({
        port:8080,
        hostnames: ['someonehost', 'otheronehost'],
        mapSignname: signname => `../signs/${signname}`,
        mapHostname: (hostname, pathname) => `../hosts/${hostname}`,
        mapPathname: (pathname, hostname) => `${pathname}`
    })

Control

  • After the server has been created, it can be closed directly:

    robusto({
      hostnames: ['localhost']
    })
    .then(server => (
      server.close(),
      process.exit(0)
    ))
  • After the server has been created, runtime errors can be catched:

    robusto({
      hostnames: ['localhost']
    })
    .then(server =>
      server
      .on('error', error => (
        console.error(error),
        process.exit(1)
      ))
    )
  • After the server has not been created, throwed error can be catched:

    robusto({
      hostnames: ['localhost']
    })
    .catch(error => (
      console.error(error),
      process.exit(1)
    ))

CLI usage

Configuration file

robusto.config.js exports the object containing the options for the execution command starting a server. Detailed in Options below.

Execution command

robusto starts a server with the options contained in the object exported from the configuration file.

Example

Configure

Create a configuration file named robusto.config.js:

export default {
  port:8080,
  hostnames: ['machine'],
  mapSignname: signname => `../${signname}`,
  mapHostname: (hostname, pathname) => hostname,
  mapPathname: (pathname, hostname) => pathname
}

Execute

Run robusto via package.json start/test script:

"scripts": {
  "start": "robusto"
}

Options

  • port: A Number specifies the port number to take.

  • hostnames: An Array containing the hostnames to serve.

  • mapSignname(filename): A Function mapping paths to SSL files.

  • mapHostname(hostname[, pathname]): A Function transforming request hostname according to the specified hostname and pathname.

  • mapPathname(pathname[, hostname]): A Function transforming request pathname according to the specified hostname and pathname.

Details

Limitations

  • Directory listing is not supported.

Requests

  • Resulted paths for file searchings are made up of the requested hostnames and pathnames according the transformations defined by mapHostname and mapPathname functions if defined.

  • Requested hostnames and pathnames are taken from incoming client requests.

  • Requested hostnames and pathnames are normalized by path.normalize function.

  • Requested hostnames are decoded by punycode.toUnicode function.

  • Requested pathnames are decoded by decodeURIComponent function.

Responses

  • File requests matching to directories and directory requests matching to files are redirected.

  • File requests are responded by the files available by the resulted paths.

  • Directory requests are responded by the files that are both acceptable according the order of acceptable MIME types of client's requests and found within the directories available by the resulted paths.

    • Directory requests are responded by the files with the basenames defined as index.

    • Directory requests are responded by the files with the extensions defined by mime.getExtension function which receives the acceptable MIME types of client's requests.

  • Requests are responded by the MIME type defined by mime.getType function which receives the name of the found file.

Assets

  • If the certificate file or any one of the key files is not found, the selfsigned certificate file and both key files will be regenerated via node-forge.

  • If any one of the root directories is not found, the empty one will be created.

Features

  • Deflate, Gzip, Brotli are supported and provided by zlib.

License

MIT.