1.10.0 • Published 10 months ago

server-over-express v1.10.0

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Server over Express (SoE) NPM Downloads

Description

A wrapper over express to order all things like middleware, controllers and context.

Installation

npm i server-over-express

Import in ES6 (Node/Web)

import { HTTPServer } from 'server-over-express';

Import in CommonJS

const { HTTPServer } = require('server-over-express');

Examples

Review tests for more examples

Basic server

import { HTTPServer } from 'server-over-express';

const httpServer = new HTTPServer();

console.log('Listening on port: ', httpServer.port);

Define a port

import { HTTPServer } from 'server-over-express';

const httpServer = new HTTPServer(8080);

Define a common response for all request

import { HTTPServer } from 'server-over-express';

const httpResponse: IHttpResponseConstructor = class {

    public data: unknown; //This is the property used to reply a request
    public code = 253;
    public headers = [];
    public timestamp = new Date().getTime();
                //This the response of a constructor
    constructor(response: unknown, context: IHTTPContextData) {
        this.data ={ ...this, data: response, headers: undefined };
    }
}

const httpServer = new HTTPServer(8080, httpResponse);

Handle request with controllers

import { HTTPServer, IHTTPController, HttpMethodEnum, IHTTPContextData, HTTPRequest } from 'server-over-express';

const httpServer = new HTTPServer();

const controller = ((): IHTTPController => {
    return {
        path: '/user',
        handlers: [{
            path: {
                method: HttpMethodEnum.GET,
            },
            action: async (request: HTTPRequest, context: IHTTPContextData) => {
                return { user: 'username' };
            }
        }]
    };
})();

httpServer.controllers.add(controller);

Handle request with controllers and subcontrollers

import { HTTPServer, IHTTPController, HttpMethodEnum, IHTTPContextData, HTTPRequest } from 'server-over-express';

const httpServer = new HTTPServer();
const subController: IHTTPController = {
    path: 'uploads',
    handlers: [{
        path: {
            method: HttpMethodEnum.GET,
        },
        action: async (request: HTTPRequest, context: IHTTPContextData) => {
            return [{ file: 'File-A' }, { file: 'File-B' }, { file: 'File-C' }];
        }
    }]
};
const controller = {
    path: '/user',
    handlers: [{
        path: {
            method: HttpMethodEnum.GET,
        },
        action: async (request: HTTPRequest, context: IHTTPContextData) => {
            return { user: 'username' };
        }
    }],
    controllers: [ subController ]
};

httpServer.controllers.add(controller);

/*
    ''
    \
     'user' -> { user: 'username' }
     \
      \
       'uploads' -> [{ file: 'File-A' }, { file: 'File-B' }, { file: 'File-C' }]
*/

Handling a not found error

const httpServer = new HTTPServer();
const controller: IHTTPController = {
    path: '*',
    handlers: [{
        path: {
            method: HttpMethodEnum.GET,
        },
        action: async (request: HTTPRequest, context: IHTTPContextData) => {
            return 'Not found!';
        }
    }]
};

httpServer.controller.add(controller);

Actions with status code higher than 400 will raise an error and not execute the next actions

import { HTTPServer, IHTTPController, HttpMethodEnum, IHTTPContextData, HTTPRequest } from 'server-over-express';

const httpServer = new HTTPServer();

const action: IHTTPIntermediateAction = {
                    execute: (request: HTTPRequest, context: IHTTPContextData) => {
                        context.code = 402;
                        
                    },
                    paths: {
                        include: [''],
                    }
                }

                const controller = {
                    path: '',
                    handlers: [{
                        path: {
                            method: HttpMethodEnum.GET,
                        },
                        action: async (request: HTTPRequest, context: IHTTPContextData) => {
                            context.code = 201;
                            return 'test result';
                        }
                    }]
                };

                httpServer.controllers.add(controller);
                httpServer.request.before.add(action);

Execute actions before reply a request (AUTH)

import { HTTPServer, IHTTPController, HttpMethodEnum, IHTTPContextData, HTTPRequest } from 'server-over-express';

const httpServer = new HTTPServer();

const controller = ((): IHTTPController => {
    return {
        path: '/user',
        handlers: [{
            path: {
                method: HttpMethodEnum.GET,
            },
            action: async (request: HTTPRequest, context: IHTTPContextData) => {
                return { user: 'username' };
            }
        }]
    };
})();
// This action authorized 50% of the times
const action: IHTTPIntermediateAction = {
    execute: (request: HTTPRequest, context: IHTTPContextData) => {
        const result = Math.round(Math.random());

        if (!result) {
            throw new Error('Unauthorized');
        }
    },
    paths: {
        include: [''] // For all routes
    }
}

httpServer.controllers.add(controller);
httpServer.request.before.add(action);

Execute actions after reply a request (CORS)

import { HTTPServer, IHTTPController, HttpMethodEnum, IHTTPContextData, HTTPRequest } from 'server-over-express';

const httpServer = new HTTPServer();

const controller = ((): IHTTPController => {
    return {
        path: '/user',
        handlers: [{
            path: {
                method: HttpMethodEnum.GET,
            },
            action: async (request: HTTPRequest, context: IHTTPContextData) => {
                return { user: 'username' };
            }
        }]
    };
})();
// This action add a CORS header
const action: IHTTPIntermediateAction = {
    execute: (request: HTTPRequest, context: IHTTPContextData) => {
        context.headers.push({ key: 'Access-Control-Allow-Origin', value: '*' });
        context.test = 'TEST';
    },
    paths: {
        include: [''] // For all routes
    }
}

httpServer.controllers.add(controller);
httpServer.request.after.add(action);

Close server

import { HTTPServer } from 'server-over-express';

const httpServer = new HTTPServer();

(async () => {
    await httpServer.close();
});
1.9.1

10 months ago

1.9.0

10 months ago

1.9.4

10 months ago

1.9.3

10 months ago

1.9.2

10 months ago

1.10.0

10 months ago

1.8.0

12 months ago

1.7.0

12 months ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.2.0

2 years ago

1.3.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago