@expresso/server
v1.1.4
Published
@expresso's simple, funny, opinated and http-based server wrapper for @expresso/app
Downloads
69
Readme
Server
Server middleware for Expresso
Summary
What is this
Expresso is opinionated, so this means we export our own server with our own configs, which are basically none. This package exposes a single simple Node HTTP Server in order to bind express to it.
Basic usage
Install it:
$ npm i @expresso/server
Import and use:
import { privKey } from './config'
import expresso from '@expresso/app'
import server from '@expresso/server'
import { IExpressoConfigOptions } from '@expresso/app'
interface IAppConfig extends IAuthConfig, IExpressoConfigOptions {}
const appFactory = expresso((app, config: IAppConfig, environment) => {
// your expresso app
})
server.start(appFactory, { name: 'appname', jwt: { algorithms: ['HS256'], audience: 'audience', issuer: 'your-issuer', secret: privKey } })
API
The server exposes a single start
method with the following signature:
async function start<TConfig extends IServerConfig> (appFactory: IExpressoAppFactory<TConfig>, options: TConfig, serverTransformer?: IServerTransformer)
The appFactory
is your expresso app.
Options
The options object follows this signature:
export interface IServerConfig {
name: string
server?: {
printOnListening?: boolean
binding?: {
ip: string
port: number
}
}
}
- Name: Server or app name
- Server [optional]:
- printOnListening (boolean): Enables or disables the informative text line
Expresso server listening on...
- binding:
- ip (string): The IP on which the server will bind to
- port (number): The port the server will listen to
- printOnListening (boolean): Enables or disables the informative text line
Transformers
The server exposes a serverTransformer
parameter, this is made in order to allow you to change and customize your server on on the fly. It is a function with the following signature:
(server: http.Server, config: Required<IServerConfig>): Promise<void>
In this function you can modify the server
object to bind, include or change whatever configuration you need. Since objects are passed as references to functions in JS, there's no need to return it
Envs
You can also set envs to change the default PORT
and HOST
of the server.
SERVER_BINDING_PORT
orPORT
: Will have the same effect as adding{ server: { binding: { port: xxxx } } }
to your config fileSERVER_BINDING_IP
orHOST
: Will have the same effect as adding{ server: { binding: { ip: 'xxx.xxx.xxx.xxx' } } }
to your config file