Socket
Socket
Sign inDemoInstall

simple-boot-http-server

Package Overview
Dependencies
6
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-boot-http-server

back end http server frameworks


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
974 kB
Created
Weekly downloads
 

Readme

Source

SIMPLE-BOOT-HTTP-SERVER

http web server
typescript typescript license

  • URL, Method, Mapping
  • Filter
  • EndPoint
  • Advice(ExceptionHandler)

dependencies

  • simple-boot-core typescript

🚀 Quick start

npm init -y
npm install simple-boot-http-server
tsc --init --experimentalDecorators --emitDecoratorMetadata
import {HttpServerOption} from 'simple-boot-http-server/option/HttpServerOption';
import {SimpleBootHttpServer} from 'simple-boot-http-server';
import {Sim} from 'simple-boot-core/decorators/SimDecorator';
import {Route, Router} from 'simple-boot-core/decorators/route/Router';
import {GET} from 'simple-boot-http-server/decorators/MethodMapping';
import {Mimes} from 'simple-boot-http-server/codes/Mimes';
import {RequestResponse} from 'simple-boot-http-server/models/RequestResponse';
import {ReqHeader} from 'simple-boot-http-server/models/datas/ReqHeader';
import {RouterModule} from 'simple-boot-core/route/RouterModule';

@Sim() @Router({path: ''})
export class AppRouter {
    @Route({path: '/'}) @GET({res: {contentType: Mimes.ApplicationJson}})
    index(rr: RequestResponse, header: ReqHeader, routerModule: RouterModule) {
        return {name: 'visualkhh'}
    }
}

const httpServerOption = new HttpServerOption({
    listen: {
        listeningListener: (server, httpServer) => {
            console.log('server on', httpServer.address());
        }
    }
});

const app = new SimpleBootHttpServer(AppRouter, httpServerOption);
app.run();

// 💥 GET /
// {"name": "visualkhh"}
npx ts-node index.ts
curl -XGET http://localhost:8081
#result: {"name": "visualkhh"}

😃 examples

URL, Method, Mapping

URL MAPPING

  • @Route({path: '/'})

METHOD MAPPING

  • @GET
  • @POST
  • @PATCH
  • @OPTIONS
  • @HEAD
  • @TRACE
  • @CONNECT
  • OTHER
    • @UrlMapping

Method Decorator options

export type MappingConfig = {
    method: HttpMethod | string,
    description?: {
        name?: string;
        detail?: string;
    };

    req?: {
        contentType?: (Mimes|string)[];
        accept?: (Mimes|string)[];
    };

    res?: {
        status?: number;
        header?: {[key: string]: string};
        contentType?: Mimes | string;
    }
    resolver?: Resolver|ConstructorType<Resolver>;
}

Filter

return

  • true: after call chain
  • false: after NoCall chain
export class FirstFilter implements Filter {
    async before(rr: RequestResponse, app: SimpleBootHttpServer): Promise<boolean> {
        console.log('filter before')
        return true;
    }
    
    async after(rr: RequestResponse, app: SimpleBootHttpServer, sw: boolean): Promise<boolean> {
        console.log('filter after')
        return true;
    }    
}
const httpServerOption = new HttpServerOption({
    filters: [new FirstFilter()],
    listen: {
        listeningListener: (server, httpServer) => {
            console.log('server on', httpServer.address());
        }
    }
});

const app = new SimpleBootHttpServer(AppRouter, httpServerOption);
app.run();
// 💥 CALL
// filter before
// filter after

Advice(ExceptionHandler)

import {NotFoundError} from './NotFoundError';

@Sim
export class Advice {
    @ExceptionHandler({type: NotFoundError})
    catch(rr: RequestResponse, e: any) {
        rr.resSetHeaders({error: 'NotFound'});
        console.log('exception--->', rr.reqUrl, e);
    }
}

const httpServerOption = new HttpServerOption({
    globalAdvice: Advice,
    noSuchRouteEndPointMappingThrow: rr => new NotFoundError(),
    listen: {
        listeningListener: (server, httpServer) => {
            console.log('server on', httpServer.address());
        }
    }
});

const app = new SimpleBootHttpServer(AppRouter, httpServerOption);
app.run();
// 💥 CALL
// exception---> NotFound

EndPoint

@Sim
class CloseEndPoint implements EndPoint {
    async endPoint(rr: RequestResponse, app: SimpleBootHttpServer) {
        console.log('close request response')
    }
}
@Sim
class ErrorEndPoint implements EndPoint {
    async endPoint(rr: RequestResponse, app: SimpleBootHttpServer) {
        console.log('error')
    }
}
const httpServerOption = new HttpServerOption({
    errorEndPoints: [CloseEndPoint],
    closeEndPoints: [ErrorEndPoint],
    listen: {
        listeningListener: (server, httpServer) => {
            console.log('server on', httpServer.address());
        }
    }
});

const app = new SimpleBootHttpServer(AppRouter, httpServerOption);
app.run();
// 💥 normal CALL
// close request response
// 💥 error
// error

Keywords

FAQs

Last updated on 27 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc