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

@welib/web-server

v0.0.2

Published

flexible web server setup

Downloads

2

Readme

The @welib/web-server package exports a function that can be used to setup an http.Server or https.Server object, depending on the options.

@welib/web-server

This package exports a single function (createServer), which accpets two optional arguments: an options object and a request listener function. This is intended to work very much like the standard Node.js createServer functions exported by the http and https modules, but the supported options are limited and different.

Example: negotiate TLS

In the following example, the configuration has a tls key, so the createServer function will return an https.Server. Otherwise, the function would return an http.Server. The Server instance will have a .secure property set to true if TLS was provided.

When configuring TLS, the function will read files from the paths specified in the .tls.cert, .tls.key, .tls.ca, and .tls.pfx options. These files must exist and be readable if set in the options.

import {createServer} from "@welib/web-server";

const cert = "/path/to/cert.pem";
const key = "/path/to/key.pem";
const config = {tls: {cert, key}};
const server = createServer(config, requestListener);
const port = server.tls ? 443 : 80;

server.listen(port);

function requestListener(req, res) {
  res.writeHead(404);
  res.end("Not Found\n");
}

Example: handling Forwarded header

If the server is running behind a trusted proxy, the trust_proxy option can be used to process the Forwarded header. This MUST NOT be used unless you are running behind a proxy which is setting the Forwarded header in a safe manner.

In the following example, the server will response with the requested URL. If a Forwarded header is provided, the URL will use it to determine the URL.

import {createServer} from "@welib/web-server";

const config = {trust_proxy: true};
const server = createServer(config, requestListener);

server.listen(8000, "127.0.0.1");

function requestListener(req, res) {
  const proto = req.connection.encrypted ? "https" : "http";
  const {headers: {host}, url: path} = req;
  const url = new URL(`${proto}://${host}${path}`);

  if (req.forwarded.proto) url.protocol = `${req.forwarded.proto}:`;
  if (req.forwarded.host) url.port = "", url.host = req.forwarded.host;

  res.writeHead(200);
  res.end(`${url}\n`);
}