A HTTP server instance.

interface IHttpServer {
    errorHandler: HttpErrorHandler;
    instance: any;
    isEgoHttpServer: true;
    notFoundHandler: HttpNotFoundHandler;
    port?: Optional<number>;
    all(path, handler): IHttpServer;
    all(path, optionsOrMiddlewares, handler): IHttpServer;
    close(): Promise<void>;
    connect(path, handler): IHttpServer;
    connect(path, optionsOrMiddlewares, handler): IHttpServer;
    controllers(): IControllersResult;
    controllers(rootDir, imports?): IControllersResult;
    controllers(options): IControllersResult;
    delete(path, handler): IHttpServer;
    delete(path, optionsOrMiddlewares, handler): IHttpServer;
    emit(event, context): Promise<any>;
    get(path, handler): IHttpServer;
    get(path, optionsOrMiddlewares, handler): IHttpServer;
    head(path, handler): IHttpServer;
    head(path, optionsOrMiddlewares, handler): IHttpServer;
    listen(port?): Promise<void>;
    on(event, handler): IHttpServer;
    on(event, handler): IHttpServer;
    options(path, handler): IHttpServer;
    options(path, optionsOrMiddlewares, handler): IHttpServer;
    patch(path, handler): IHttpServer;
    patch(path, optionsOrMiddlewares, handler): IHttpServer;
    post(path, handler): IHttpServer;
    post(path, optionsOrMiddlewares, handler): IHttpServer;
    put(path, handler): IHttpServer;
    put(path, optionsOrMiddlewares, handler): IHttpServer;
    setErrorHandler(handler): IHttpServer;
    setNotFoundHandler(handler): IHttpServer;
    test(options?): Promise<IHttpServerTestResult>;
    trace(path, handler): IHttpServer;
    trace(path, optionsOrMiddlewares, handler): IHttpServer;
    use(...middlewares?): IHttpServer;
    (request, response): void;
}
  • A HTTP server itself is a HTTP request listener, which can be used in any compatible server instance, like Node HTTP, e.g.

    Parameters

    • request: IncomingMessage
    • response: ServerResponse<IncomingMessage>

    Returns void

    Example

    import createServer from '@egomobile/http-server'
    import { createServer as createHTTPServer } from 'http'

    const app = createServer()
    const server = createHTTPServer(app)

    server.listen(8080, () => {
    console.log('Server is listing')
    })

Properties

errorHandler: HttpErrorHandler

Gets the current error handler.

instance: any

The underlying low-level server instance, which is only available if server is listening, otherwise it is null.

Mostly this is a Server instance from http module.

isEgoHttpServer: true

Indicates if that instance is an e.GO HTTP server.

Example

import assert from 'assert'
import createServer from '@egomobile/http-server'

const app = createServer()

assert.strictEqual(app.isEgoHttpServer, true)
notFoundHandler: HttpNotFoundHandler

Gets the current "not found" handler.

port?: Optional<number>

The current TCP port or (undefined) if server is not running.

Methods

  • Closes / stops the server.

    Returns Promise<void>

    Example

    import createServer from '@egomobile/http-server'

    const app = createServer()

    await app = server.listen()
    console.log('Server is listening')

    setTimeout(() => {
    app.close().catch(console.error)
    }, 60000)
  • Starts listening for connections.

    Parameters

    • Optional port: Nilable<string | number>

      The custom TCP port. Default is 8080 in development mode (NODE_ENV), otherwise 80.

    Returns Promise<void>

    Example

    import createServer from '@egomobile/http-server'

    const app = createServer()

    await app.listen() // if NODE_ENV === 'development', port is 8080
    // otherwise port is 80

    await app.listen(5979) // explicit port 5979
  • Sets a custom error handler.

    Parameters

    Returns IHttpServer

    Example

    import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'

    const app = createServer()

    app.setErrorHandler(async (error: any, request: IHttpRequest, response: IHttpResponse) => {
    const errorMessage = Buffer.from('SERVER ERROR: ' + String(error))

    if (!response.headersSent) {
    response.writeHead(500, {
    'Content-Length': String(errorMessage.length)
    })
    }

    response.write(errorMessage)
    response.end()
    })

    app.get('/', async (request: IHttpRequest, response: IHttpResponse) => {
    throw new Error('Something went wrong')
    })

    await app.listen()
  • Sets a new 'not found' handler.

    Parameters

    Returns IHttpServer

    Example

    import createServer, { IHttpRequest, IHttpResponse } from '@egomobile/http-server'

    const app = createServer()

    app.setNotFoundHandler(async (request: IHttpRequest, response: IHttpResponse) => {
    const errorMessage = Buffer.from(`The page ${request.url} could not be found`)

    if (!response.headersSent) {
    response.writeHead(404, {
    'Content-Length': String(errorMessage.length)
    })
    }

    response.write(errorMessage)
    response.end()
    })

    // ... your routes

    await app.listen()
  • Adds one or more global middlewares.

    Parameters

    • Optional Rest ...middlewares: HttpMiddleware[]

      The middlewares to add.

    Returns IHttpServer

    Example

    import assert from 'assert'
    import createServer from '@egomobile/http-server'

    const app = createServer()

    assert.strictEqual(app.isEgoHttpServer, true)

    app.use(async (request: any, response: any, next) => {
    request.foo = '1'
    next()
    }, async (request: any, response: any, next) => {
    // foo is currently '1'
    request.foo += 2
    next()
    })

    app.get('/', async (request: any, response: any) => {
    response.write(String(request.foo === '12'))
    })

Generated using TypeDoc