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

runsv-http

v2.0.0

Published

A runsv http server wrapper

Downloads

24

Readme

pipeline status coverage report

runsv-http

A runsv wrapper around http.Server.

Usage

Simple express app


const runsv = require('runsv').create();
const createHTTPService = require('runsv-http');
const express = require('express');
const app = express();
const PORT = 3000;

/* set up your app */
app.get('/', (req, res) => res.end('hello'));

// configure the runsv-http service
const myWeb = createHTTPService('myWeb') // optionally you can set a custom name
	.createServer(app)  // same signature as http.createServer()
	.listen(PORT); // same signature as server.listen. DO NOT include the callback.

runsv.addService(myWeb);
runsv.start(function(err){
	const {myWeb} = runsv.getClients();
	console.log(`listening at port ${PORT}`);
});
//...

API

In node you usually create an instance of an http server like this:

const http = require('http');
const port = 3000;
function listener(req, res){
	res.end('hello');
}
const server = http.createServer(listener);
server.listen(port, function(err){
	console.log(`listening at ${port}`);
});

Both createServer() and listen() accept optional params. The goal of this API is to be as close as possible to the original node API.

const runsv = require('runsv').create();
const port = 3000;
const createService = require('runsv-http');

function listener(req, res){
	res.end('hello');
}

const server = createService()
	.createServer(listener)
	.listen(port);

runsv.addService(server);
runsv.start(function(err){
	console.log(`listening at ${port}`);
});
//...
  • #createService([name]) returns a runsv service wrapper around original nodejshttp.server.
    • [name='http'] runsv service name. Optional. By default is http.
    • Returns: a runsv service. This service also exposes #createServer([options][,requestListener]) to mimic original node API
  • #createServer([options][,requestListener] mimics the original function
    • Returns: previous runsv service plus the #listen() function.
  • #listen() check the options of the original function. Do not include the callback instead, use runsv start event.
    • Returns: previous runsv service.

Examples

Random port

const runsv = require('runsv').create();
const createService = require('runsv-http');

function listener(req, res){
	res.end('hello');
}

const server = createService()
	.createServer(listener);

runsv.addService(server);
runsv.start(function(err){
	const {port} = services.getClients().http.address();
	console.log(`listening at ${port}`);
});
//...

Handle on request

const runsv = require('runsv').create();
const createService = require('runsv-http');

function listener(req, res){
	res.end('hello');
}

// As with the original http service we can define 
// a listener later...
const server = createService();

runsv.addService(server);
runsv.start(function(err){
	const {http} = services.getClients();
	// Respond to request events
	http.on('request', listener);
	const {port} = http.address();
	console.log(`listening at ${port}`);
});
//...

See more examples

Stop behavior

#stop(callback) just invokes server.close([callback]).

Bear in mind that stopping (or closing) this service will not terminate the http server immediately.
http-terminator docs explains why.

When you call server.close(), it stops the server from accepting new connections, but it keeps the existing connections open indefinitely. This can result in your server hanging indefinitely due to keep-alive connections or because of the ongoing requests that do not produce a response. Therefore, in order to close the server, you must track creation of all connections and terminate them yourself.

-- http-terminator docs Tracking the connections and terminating them in a gracefully way is beyond the scope of this service wrapper.
There are modules like http-terminator, http-graceful-shutdown or http-close that will do the heavy lifting for you.

// Example: Gracefully terminating connections with http-close
const runsv = require('runsv').create();
const createHTTPService = require('runsv-http');
const httpClose = require('http-close');
const express = require('express');
const app = express();

app.get('/', (req, res) => res.end('hello'));

const httpService = createHTTPService('server').createServer(app);

runsv.addService(httpService);
runsv.start(function(err){
	const {server} = runsv.getClients();
	// modify server.close behavior to gracefully terminate connections
	httpClose({ timeout: 2000 }, server);
});
//...