1.1.2 ā€¢ Published 2 months ago

@knowdev/express v1.1.2

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
2 months ago

KnowDev Express šŸš…

Express on AWS Lambda toolkit

ā„¹ļø Overview

Encapsulates project opinions on JSON:API, logging, and debugging

  • projectHandler() function should wrap all routes
  • The handler will bootstrap logging provided by @knowdev/log
  • The inbound request will be logged
  • The outbound response will be logged
  • The outbound response will be decorated with project headers
  • The handler will catch any @knowdev/errors and respond properly
  • The handler will catch any uncaught errors and respond with a 500 (from @knowdev/errors)

šŸ“‹ Usage

Installation

npm install --save @knowdev/express

Environment Variables

For @knowdev/log:

LOG_LEVEL=all|trace|*debug*|info|warn|error|fatal|silent

# Project metadata
PROJECT_ENV=dev
PROJECT_KEY=mayhem
PROJECT_VERSION=0.1.0

# Project secret will let tests bypass authentication
PROJECT_SECRET=

# Danger!
PROJECT_UNAVAILABLE=false

āš ļø Danger! Setting PROJECT_UNAVAILABLE=true will cause the handler to always throw 503 Unavailable errors.

Example

const { log, projectHandler } = require("@knowdev/express");

router.all(
  "*",
  projectHandler((req, res) => {
    log.debug("Entering my handler logic");
    const response = { hello: "world" };
    res.json(response);
  })
);

module.exports = router;

šŸ“– Reference

Project Handler

const { projectHandler } = require("@knowdev/express");

const myMiddleware = projectHandler((req, res, next) => {
    next();
});

const myRouteHandler = projectHandler((req, res) => {
    const response = { hello: "world" };
    res.json(response);
});

router.all("*", myMiddleWare, myRouteHandler);

šŸ’” Note: because the project is opinionated about returning JSON, the project handler assumes res.json() is going to be called,

Handler Context

Use the context object to pass options

const { projectHandler } = require("@knowdev/express");

const myFunction - (req, res) => {
  // ...
}

const context = {
  name: "myFunction",
  // ...
}

const myRouteHandler = projectHandler(myFunction, context);

router.all("*", myRouteHandler);
OptionDefaultDescription
locals{}req.locals will be populated to these values, executing any functions
nameundefinedName used in logging
setup[]Functions to run before the handler
teardown[]Functions to run after the handler
unavailablefalse \|\| process.env.PROJECT_UNAVAILABLEThrow 503 instead of responding. For unusual maintenance situations only (you're welcome)
validate[]Functions to run before the handler, which MUST throw their own errors if they fail
versionprocess.env.PROJECT_VERSIONVersion used in logging

Project Headers

The following headers will be included based off the following values:

HeaderValue
X-Powered-Byknowdev.studio
X-Project-Environmentprocess.env.PROJECT_ENV
X-Project-InvokeAWS Lambda invoke UUID
X-Project-Keyprocess.env.PROJECT_KEY
X-Project-Versionprocess.env.PROJECT_VERSION

Logging

const { log } = require("@knowdev/express");

// Log by level
log.trace();
log.debug();
log.info();
log.warn();
log.error();
log.fatal();

// Special log variables:
log.var({ response });

// Chain for different levels:
log.trace.var({ response });
log.debug.var({ response }); // Default
log.info.var({ response });
log.warn.var({ response });
log.error.var({ response });
log.fatal.var({ response });

Learn more at @knowdev/log

Routes

echoRoute

const express = require("express");
const { echoRoute } = require("@knowdev/express");

const app = express();
app.use("/echo", echoRoute);
app.use("/echo/*", echoRoute);

Responds by echoing back details of your original submission, helpful for debugging. Some special routes return special messages:

RouteResponse
error/400Bad request error
error/403Forbidden error
error/404Not found error
error/500Internal error
error/502Bad gateway error
error/503Unavailable error
error/504Gateway timeout error
error/unhandledUnhandled error
error/*Not found error
log/errorLogs error and exits
log/fatalLogs fatal and exits
log/warnLogs warn and exits
log/bothLogs warn followed by error and exits

Note these error routes are in addition to the path you may be using to specify the echo route. If you follow the above example of using /echo/*, your full route would be /echo/error/503

httpRoute

const { httpRoute } = require("@knowdev/express");
const HTTP = require("@knowdev/http");

const app = express();
app.use("/bogus", httpRoute(HTTP.CODE.NOT_FOUND));
app.use("/bogus/*", httpRoute(HTTP.CODE.NOT_FOUND));

Returns an empty body for 200, no content for 204, and a proper error message if possible for 4XX and 5XX.

Accepts any context that can be passed to the projectHandler

app.use("/*", httpRoute(HTTP.CODE.BAD_REQUEST, { name: "badRequestCatchAll" }));

Functions

summarizeRequest

Summarizes the request, for the debugging. Used by the echo route

const { summarizeRequest } = require("@knowdev/express");

log.var({ req: summarizeRequest(req) })

summarizeResponse

Summarizes the response, for the debugging. Used in logging

const { summarizeResponse } = require("@knowdev/express");

log.var({ res: summarizeResponse(res) })

šŸ“ Changelog

  • v1.1.0: Adds locals, setup, teardown, validate
  • v1.0.6: Handles async errors
  • v1.0.0: First stable release
  • v0.8.0: Log routes
  • v0.7.0: JSON Tagging
  • v0.6.0: JSON logging
  • v0.5.0: more multi-call safe
  • v0.4.0: httpRoute
  • v0.3.0: Headers, PROJECT_UNAVAILABLE
  • v0.2.0: Logging, special echo routes, summarize response
  • v0.1.0: fork from separate project (echoRoute, log, projectHandler, summarizeRequest)

šŸ“œ License

All rights reserved. Safe for use around pets

1.1.2

2 months ago

1.1.1

2 months ago

1.1.0

2 months ago

1.0.6

3 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.2

4 months ago

1.0.3

4 months ago

1.0.0

7 months ago

0.8.5

7 months ago

0.8.6

7 months ago

0.8.4-0

7 months ago

0.8.5-1

7 months ago

0.8.5-0

7 months ago

0.8.1

7 months ago

0.6.3

7 months ago

0.8.0

7 months ago

0.6.2

7 months ago

0.8.3

7 months ago

0.8.2

7 months ago

0.7.0

7 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.1

10 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago