JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q47459F
  • License MIT

express web server implemented based on clean architecture

Package Exports

  • @juniorcitizen/express-web-server

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@juniorcitizen/express-web-server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@juniorcitizen/express-web-server

express web server implemented based on clean architecture

HIGHLIGHTS

  • zero-config express.js web server written in typescript

  • based on clean architecture, implemented as an use-case and broken into layers

  • popular Express.js middlewares are available for plugged-in

  • make your own middleware or extend the server functionalities

  • router plugin system that can plug in other clean architecture use-cases

INSTALLATION

npm install --save @juniorcitizen/express-web-server
npm install --save @juniorcitizen/environment-variables

USAGE

import EnvironmentVariables from '@juniorcitizen/environment-variables'
import {
  StartWebServer,
  StartWebServerServiceFactory as ServiceFactory,
  RouterPlugIn, // router plugin
  Morgan, // middleware plugins
} from '@juniorcitizen/express-web-server'

// get env variables
const env = new EnvironmentVariables()

// factory for extending server functionalities
const serviceFactory = new ServiceFactory()

// add morgan logger plugin
serviceFactory.plugIn(new Morgan(env))

// add a router
const appRouter = new RouterPlugIn('/')
appRouter.addGetHandlers([middleware..., route handler...])

// add a second router
const testRouter = new RouterPlugIn('/test')
testRouter.addGetHandlers([middleware..., route handler...])

// attach testRouter to the appRouter
appRouter.attachChild(testRouter)

// attach appRouter to Express server
serviceFactory.plugIn(appRouter)

// instantiate server service
const startWebServer = new StartWebServer(serviceFactory)

// start server
startWebServer.execute(env)

PLUGINS

Available middlewares & extensions

  • json body parser

  • urlencoded body parser

  • response compression

  • EJS template engine

  • express-session with redis caching

  • helmet

  • serve-favicon

  • static file serving

How to roll your own middleware

import EnvironmentVariables from '@juniorcitizen/environment-variables'
import {
  StartWebServer,
  StartWebServerServiceFactory as ServiceFactory,
  AbstractServicePlugIn, // extend the plugin base class
} from '@juniorcitizen/express-web-server'

// extend and implement your plugin
class MyPlugIn extends AbstractServicePlugIn {
  // dependencies can be injected
  // through the constructor
  constructor(thing, someFunction) {
    super()
    this.thing = thing
    this.someFunction = someFunction
  }
  // an instance of the Express app is exposed
  // to the 'execute' method, enable middleware
  // and extension to the Express server
  execute(app) {
    const middleware = (req, res, next) => {
      this.someFunction()
      console.log(this.thing)
      next()
    }
    app.use(middleware)
  }
}

const env = new EnvironmentVariables() // env
const serviceFactory = new ServiceFactory() // service factory
// make your dependencies
const someFunction = () => {
  console.log('someFunction is executed')
}
const thing = 'I am the thing'
const myPlugIn = new MyPlugIn(thing, someFunction) // inst. your plugIn
serviceFactory.plugIn(myPlugIn) // plug your plugIn
const startWebServer = new StartWebServer(serviceFactory) // inst. server
// plug in some routers...
// ...
// ...
startWebServer.execute(env) // start server

ROUTER PLUGIN SYSTEM

to be completed... see the 'USAGE' section for example

API

to be completed...