Nazgul is a routing library for NodeJS express applications. The full featureset:
- controller > method level routing system
- filter attributes
- runtime SWAGGER hosting
- SWAGGER generation
To install nazgul run the following script:
yarn add -D @nazgul/hub @nazgul/core
- Create a controller class.
- Add the respective
@HttpController
decorator to it - Add the proper http decorator to the endpoint implementation
- Add the respective
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
}
}
- Configure the main entry point to use Nazgul
- Create an express application
- Initialize NazgulHub to use the server
- Import the controller
- 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");
});
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
}
}