npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

server-over-express

v1.10.0

Published

Another boilerplate for ts projects, especifically to ts npm packages

Downloads

479

Readme

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();
});