Skip to content

v20.0.2

Choose a tag to compare

@brandonlehmann brandonlehmann released this 06 Jan 22:35
· 17 commits to master since this release
d0ee112

Simple Express.js Application Wrapper

This wrapper makes it very easy to construct a new instance of an Express.js web server application using HTTP or HTTPs.

Features include:

  • Preloaded recommended headers (optional)
  • Automatic 404 handling (optional)
  • Compression (optional)
  • Auto-parsing of request bodies (all optional)
    • JSON
    • Raw
    • Text
    • URLEncoded
    • XML
  • Automatically attempts to decode the authorization header into request.authorization
    • Basic
    • Bearer
    • JWT
  • Provides a logging facility for requests or can specify a callback to receive request entries
  • Automatically resolves a number of different proxy-related fields for the client IP address into request.remoteIp in the following order:
    • Cf-Connecting-IPv6
    • X-Forwarded-For
    • Cf-Connecting-IP
    • request.ip
  • Session support via in-memory data store
    • Alternatively, specify a data store
  • Injects a unique request ID for every single request both in the log and in the response as X-Request-ID and request.id
  • Injects the amount of time elapsed between when the request is received and when the response headers are written in X-Response-Time which is also logged in the log entry.
  • Simple support for "protected" routes that allow for specifying a simple AuthenticationProvider to "protect" those routes
  • WebSocket via additional method signature of .ws('path', (socket, request) => void)
  • Simple cloudflared support to spin up test tunnels using Cloudflare
    • Note: The public URLs will be randomly generated by Cloudflare

Documentation

https://gibme-npm.github.io/webserver/

Sample Code

import WebServer, { Logger } from '@gibme/webserver';

(async() => {
    const app = WebServer({
        autoStartCloudflared: true
    });
    
    app.get('/', (request, response) => {
        return response.json({ success: true });
    })

    app.ws('/wss', (socket) => {
        socket.on('message', msg => {
            // simply echo the message back
            socket.send(msg);
        });
    });
    
    await app.start();
    
    Logger.info('Listening on: %s', app.localUrl);
    Logger.info('Listening on: %s', app.tunnelUrl);
    Logger.info('Listening on: %s', app.url);
})();