Giter Club home page Giter Club logo

nazgul's Introduction

nazgul

Nazgul is a routing library for NodeJS express applications. The full featureset:

  • controller > method level routing system
  • filter attributes
  • runtime SWAGGER hosting
  • SWAGGER generation

Get Started

To install nazgul run the following script:

yarn add -D @nazgul/hub @nazgul/core

To use the routing system:

  1. Create a controller class.
    1. Add the respective @HttpController decorator to it
    2. Add the proper http decorator to the endpoint implementation
import { HttpController, HttpGet }  from "@nazgul/hub";

@HttpController("/api/v1")
export class MyController {

    @HttpGet("/my-method")
    public async myMethod(
        req: Request,
        res: Response
    ) {
        // add your custom implementation here
    }

}
  1. Configure the main entry point to use Nazgul
    1. Create an express application
    2. Initialize NazgulHub to use the server
    3. Import the controller
    4. Start your server
import express from "express";

const server = express();

NazgulHub.addServer(server);

import { MyController } from "./my.controller";

server.listen(9000, () => {
    console.log("http://localhost:9000");
});

To use the filter attributes

At first, create your first attribute by using FilterAttributeFactory from @nazgul/routing

To install @nazgul/routing:

yarn add -D @nazgul/routing

To create a filter attribute:

import { FilterAttributeFactory } from "@nazgul/routing";

export const WithTokenOnly = (token: string) => FilterAttributeFactory.create(
    (req, res, next) => {
        const requestToken = req.query.token;
        if (token !== requestToken) {
            res.status(401).send();
            return false;
        }
        return true;
    }
)

The filter attributes can return boolean or Promise<boolean>, so DB reads, 3rd party API calls are possible. For application-level behavoiur for example request-logging or error handling there is no such way in Nazgul by design. For such features it is strongly suggested to use express' native middleware functionality.

Then add your newly create filter to one of your controller methods.

import { HttpController, HttpGet }  from "@nazgul/hub";

@HttpController("/api/v1")
export class MyController {

    @HttpGet("/my-method")
    @WithTokenOnly("my-secret-token")
    public async myMethod(
        req: Request,
        res: Response
    ) {
        // add your custom implementation here
    }

}

nazgul's People

Contributors

kfarkashu avatar

Watchers

 avatar

nazgul's Issues

rm nextfn

By design nazgul does not support NextFunction - would be better to completely remove it and store the ref in runtime ctx to support outscaling.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.