npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-hosting

v1.3.3

Published

Simple reverse proxy for hosting multiple apps in the same server

Downloads

7

Readme

simple-hosting

Simple reverse proxy for hosting multiple apps in the same server

npm i simple-hosting

Usage

Let's say you already have two apps running in a server: http://127.0.0.1:1337 and http://127.0.0.1:3000

Normally run this in a public server with DNS, and all app domains pointing to the same server IP.

const Hosting = require('simple-hosting')

const hosting = new Hosting()

// Replace with your own domains
hosting.add('a.leet.ar', { destination: 'http://127.0.0.1:1337' })
hosting.add('b.leet.ar', { destination: 'http://127.0.0.1:3000' })

hosting.add('c.leet.ar', {
  destination: 'http://127.0.0.1:3000', // Frontend
  location: {
    '/api': 'http://127.0.0.1:1010' // Backend
  }
})

await hosting.listen({ securePort: false })

// Try making requests to http://127.0.0.1:80 and setting the Host header accordingly
// Or if this is in a server then use your browser to request the actual domains

Feel free to open an issue if you have any doubt.

API

const hosting = new Hosting([options])

Creates a pair of servers that uses the Host header to dynamically load different apps.

{
  log: 0, // Enable logging (0=disabled, 1=requests, 2=SNI)
  behindProxy: false, // If hosting is behind CloudFlare or NGINX then enable this option
  auth: String, // Secret token that must be sent in the request header "x-simple-hosting"
  certbot: true // Handle renewal of certificates
}

If you're not behind any kind of proxy then don't worry about the auth option.

If you're behind CloudFlare, NGINX, etc then to avoid spoofing headers you must use the auth option.

CloudFlare authentication:

  • Go to Rules -> Transform Rules -> Modify Request Header.
  • Create a rule for all incoming requests.
  • Set the header name as x-simple-hosting with your secret auth token as value.

NGINX authentication:

  • Configure it like proxy_set_header x-simple-hosting <secret-auth-token>

Certbot

Hosting can automatically handle renewals for any domain even if not managed by Hosting itself.

Issue a certficiate:

sudo certbot certonly --webroot --webroot-path /tmp/letsencrypt -d sub.example.com

Let it auto-renew with its cron or manually run it:

sudo certbot renew

hosting.add(servername, options)

Add a new app to the hosting by its domain name.

Available options:

{
  destination: String, // Target URL (required)
  location: Object, // Extra "starts with" URL matches
  cert: String, // Eg fullchain.pem
  key: String, // Eg privkey.pem
  certbot: false, // Use default paths for Certbot (ignores `cert` and `key` options)
  behindProxy, // Inherits the Hosting value by default (pass false to disable)
  auth // Inherits the Hosting value by default (pass null to disable)
}

cert and key are used to create the secure context for the HTTPS server.

Files will be watched for changes to auto-update the secure context.

If you use CloudFlare in front of Hosting then you don't need to set the certificate and key.

Otherwise consider using Certbot.

hosting.edit(servername, [options])

Change the app configuration in real-time. For example, you could change the destination URL.

Same options as for adding an app.

await hosting.listen([options])

Start listening for new requests. Each hosting has two servers (HTTP and HTTPS).

Available options:

{
  insecurePort: 80,
  securePort: 443 // Pass `false` to disable the HTTPS server
}

License

MIT