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
Express.js web server written in typescript
based on clean architecture
popular Express.js middlewares are available as plug-ins
accept external middleware plug-ins and server extensions
plug-in routing system
INSTALLATION
npm install --save @juniorcitizen/express-web-serverUSAGE
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
const PROD_MODE = process.env.NODE_ENV==='production'
serviceFactory.plugIn(new Morgan({PROD_MODE}))
// add a router
const appRouter = new RouterPlugIn('/')
appRouter.addAllHandlers([middleware..., route handler...])
// add a second router
const testRouter = new RouterPlugIn('/test')
testRouter.addPostHandlers([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({PORT: 3000})PLUGINS AND EXTENSIONS
Available plugIns & extensions
Redis cached server-side session: Uses ioredis and connect-redis
TO DO:
How to roll your own plugIns & extensions
import EnvironmentVariables from '@juniorcitizen/environment-variables'
import {
StartWebServer,
StartWebServerServiceFactory as ServiceFactory,
AbstractServicePlugIn, // extend the plugin base class
} from '@juniorcitizen/express-web-server'
// example of a global route-middleware plugIn
class MyPlugIn extends AbstractServicePlugIn {
// dependencies can be injected
// through the constructor
constructor(thing, someFunction) {
super()
this.thing = thing
this.someFunction = someFunction
this._middlware = (req, res, next) => {
this.someFunction()
console.log(this.thing)
next()
}
}
// expose the '_middleware' private member
// useful if this middleware is used on particular routes (non-global)
// ***implementation of this getter is required if writing typescript***
// ***for global middlewares, just return undefined***
getter middleware() {
return this._middleware
}
// an instance of the Express app is exposed
// to the 'execute' method, enable middleware
// and extension to the Express server
// ***implementation of this method is required***
execute(app) {
app.use(this._middleware)
}
}
const env = new EnvironmentVariables() // env
const serviceFactory = new ServiceFactory() // service factory
// make your plug-in dependencies
const someFunction = () => {
console.log('someFunction is executed')
}
const thing = 'I am the thing'
const myPlugIn = new MyPlugIn(thing, someFunction) // instantiate your plug-in
serviceFactory.plugIn(myPlugIn) // register your plug-in
const startWebServer = new StartWebServer(serviceFactory) // instantiate server
// setup and plug-in your routers...
// ...
// ...
startWebServer.execute(env) // start serverROUTER PLUGIN SYSTEM
to be completed... see the 'USAGE' section for example
API
to be completed... see the 'USAGE' section for example