
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@trust/http-service
Advanced tools
Modular HTTP Services
This package codifies a design pattern for defining reusable HTTP services.
It's desireable in many cases to reuse encapsulated bundles of server behavior. For example, library authors implementing an open standard may wish to export an object that is completely responsible for behavior within it's scope and entirely self-contained, such that integrators have virtually nothing to do other than import the code and pass a few configuration parameters.
const express = require('express')
const SomeService = require('some-service')
let server = express()
let service = SomeService.create(settings)
// mount the service into the new Express server
server.use(service.router)
// register additional route handlers
server.get('/', (req, res) => {
res.send('Unlike SomeService\'s endpoints, I am specific to this server')
})
server.listen(process.env.PORT || 3000)
$ npm install --save @trust/http-service
const { BaseRequest, HTTPService } = require('@trust/http-service')
A request handler is a class which optionally extends
BaseRequest and defines, at a bare minimum, a static route
getter and a static handle
method. The route
getter describes the handler
to the service so it can be wired up to an Express router. The handle
method
is the route handler, with a special extra argument service
. At runtime, the
instantiated HTTPService
will be passed to this argument. This is a form of
dependency injection.
class AlphaRequest extends BaseRequest {
/**
* The static `route` method defines the information required to
* register the request handler with an Express router.
*/
static get route () {
return {
path: '/alpha',
method: 'GET'
}
}
/**
* The static `handle` method defines endpoint behavior. This method takes
* three arguments. The first two are the IncomingRequest and HTTPResponse
* objects to be passed by Express. The third argument allows state and
* dependencies associated with the respective service to be injected.
*/
static handle (req, res, service) {
res.status(418).send(`I am a teapot. My name is ${service.name}`)
}
}
Note that there is no
next
argument tohandle(req, res, service)
. This is by design. Services are meant to be self-contained and responsible for every possible outcome of each request within their scope.
This extended BaseRequest
may look like boilerplate without a purpose until
we consider a more complex request.
The main advantage of defining a request handler as a class is that we can decompose very complex request logic with mixed synchronicity into smaller functions that can be recomposed to represent complicated control flows using promise chains and share state between them using a request instance. This is a helpful technique for avoiding bad patterns with callbacks and middleware stacks.
class BravoRequest extends BaseRequest {
/**
* Route definition
*/
static get route () {
return {
path: '/bravo',
method: 'GET'
}
}
/**
* A more complex request handler, with promise-based control flow
* and state.
*/
static handle (req, res, service) {
let request = new BravoRequest({ req, res, service })
Promise.resolve()
.then(() => request.step1())
.then(() => request.step2())
.then(() => request.step3())
.catch(err => request.error(err))
}
step1 () {
// do something synchronous
}
step2 () {
// do something asynchronous
}
step3 () {
let {res} = this
if (!this.isOk()) {
return this.badRequest()
}
res.send('asynchronously, with feeling')
}
isOk () {
return true
}
}
Here, handle
instantiates the request handler class, creating an object
representing all the state and behavior required to handle this request.
Handlers should never mutate the req
argument, as is common (and bad) practice
in Node.js development. Our request handler instance provides a safer scope for
accessing mutable data that won't come into conflict with any state defined by
middleware upstream.
This technique makes it possible to decompose a complex route handler into a series of methods that are easier to understand and test.
The BaseRequest
class provides common response and error handling methods.
class MyService extends HTTPService {
/**
* The `handlers` method returns an array of request handler classes.
*/
get handlers () {
return [
AlphaRequest,
BravoRequest
]
}
}
Copyright (c) 2017 Anvil Research, Inc.
FAQs
Modular HTTP Services for Express
The npm package @trust/http-service receives a total of 1 weekly downloads. As such, @trust/http-service popularity was classified as not popular.
We found that @trust/http-service demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.