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

@overlook/plugin-serve-http

v0.3.4

Published

Overlook framework HTTP server plugin

Downloads

10

Readme

NPM version Build Status Dependency Status Dev dependency Status Greenkeeper badge Coverage Status

Overlook framework HTTP server plugin

Part of the Overlook framework.

Usage

Use to add an HTTP server to an application.

The server is a basic NodeJS HTTP server with no middleware - middleware needs to be added by other plugins.

Typically, you would use this plugin on the root route of the application and use something like @overlook/plugin-path to route the requests to the right route in the router tree.

But it's also possible to create multiple servers listening on different ports on different routes, or use another plugin to add e.g. a WebSocket server too.

Set port

Set [PORT] or define [GET_PORT]() method.

const Route = require('@overlook/route');
const httpPlugin = require('@overlook/plugin-serve-http');
const { PORT } = httpPlugin;

const HttpRoute = Route.extend( httpPlugin );

const router = new HttpRoute( {
  [PORT]: 3000
} );
const Route = require('@overlook/route');
const httpPlugin = require('@overlook/plugin-serve-http');
const { GET_PORT } = httpPlugin;

const HttpRoute = Route.extend( httpPlugin );

class HttpOnPort3000Route extends HttpRoute {
  [GET_PORT]() {
    return 3000;
  }
}

const router = new HttpOnPort3000Route();

Start server

plugin-serve-http uses @overlook/plugin-start.

Start the server by using [START]() method provided by @overlook/plugin-start.

const { START } = require('@overlook/plugin-start');
await router[START]();

Server will be started up last, after any child routes with [START_ROUTE]() methods have completed start-up, so the whole app is ready to serve requests before the server starts.

Stop server

Stop the server by using [STOP]() method provided by @overlook/plugin-start.

const { STOP } = require('@overlook/plugin-start');
await router[STOP]();

Shutdown is graceful. Server immediately stops accepting new connections, and closes all current connections. Any requests currently in flight will be allowed to complete before their connection is closed.

Server will be stopped before any child routes with [STOP_ROUTE]() methods have commenced their shutdown. This prevents disruption of last requests before shutdown.

Handling requests

When the server receives requests, .handle() will be called on the route with a request object.

Request objects have the following properties:

  • [REQ_TYPE]: 'HTTP'
  • [REQ]: NodeJS native IncomingMessage object
  • [RES]: NodeJS native ServerResponse object
  • [METHOD]: HTTP method e.g. 'GET', 'POST'
  • [URL]: Request URL (not including protocol, host, or port) e.g. '/abc/def?foo=bar'
  • [URL_OBJ]: Parsed URL object
  • [PATH]: Request path (not including query string) e.g. '/abc/def'
  • [QUERY_STR]: Request query string e.g. '?foo=bar'
  • [QUERY]: Query object e.g. { foo: 'bar' }

All the above symbols ([REQ] etc) are exported as properties of this plugin, except [REQ_TYPE] and [PATH] which are exported by @overlook/plugin-request.

Typically, you'd use something like @overlook/plugin-path to handle requests, but you can write your own handler too.

Request body, auth and cookies are not parsed - you need to parse them yourself from [REQ], or use plugins to do this.

const Route = require('@overlook/route');
const httpPlugin = require('@overlook/plugin-serve-http');
const { PORT, METHOD, URL, RES } = httpPlugin;

const HttpRoute = Route.extend( httpPlugin );

class MyHttpRoute extends HttpRoute {
  async handle( req ) {
    // Delegate to superior handlers
    // NB No `await` here
    const result = super.handle( req );
    if ( result != null ) return result;

    // Some async processing
    const responseBody = await Promise.resolve(
      `Serving ${req[METHOD]} ${req[URL]}`
    );

    // Send response
    const res = req[RES];
    res.end( responseBody );
  }
}

const router = new MyHttpRoute( {
  PORT: 3000
} );

Multiple servers

Servers can be attached to any route in the router tree, not just the root.

However, to ensure [START] and [STOP] calls are propogated through the router tree to reach these routes, make sure the root route and all intermediary routes use the @overlook/plugin-start plugin.

Versioning

This module follows semver. Breaking changes will only be made in major version updates.

All active NodeJS release lines are supported (v10+ at time of writing). After a release line of NodeJS reaches end of life according to Node's LTS schedule, support for that version of Node may be dropped at any time, and this will not be considered a breaking change. Dropping support for a Node version will be made in a minor version update (e.g. 1.2.0 to 1.3.0). If you are using a Node version which is approaching end of life, pin your dependency of this module to patch updates only using tilde (~) e.g. ~1.2.3 to avoid breakages.

Tests

Use npm test to run the tests. Use npm run cover to check coverage.

Changelog

See changelog.md

Issues

If you discover a bug, please raise an issue on Github. https://github.com/overlookjs/plugin-serve-http/issues

Contribution

Pull requests are very welcome. Please:

  • ensure all tests pass before submitting PR
  • add tests for new features
  • document new functionality/API additions in README
  • do not add an entry to Changelog (Changelog is created when cutting releases)