Create an API

Create an fast and easy to use API using AbyssMonitor features

AbyssMonitor provide tools to setup an easy API with NodeJS and TypeScript. Tools are designed to works with Express.

Setup

Create your first Middleware

import { Request, Response, NextFunction } from 'express';
import * as contentType from 'content-type';
import { IMonitorLocals, Logger, Middleware } from '@abyss-project/monitor';

export const ALLOWED_REQUEST_TYPES = [
  'application/json',
  'multipart/form-data',
  'application/x-www-form-urlencoded',
];

@Middleware()
export default class MyMiddleware {
  private static readonly logger = new Logger({});

  public async run(
    req: Request,
    res: Response<unknown, IMonitorLocals>,
    next: NextFunction,
  ): Promise<void> {
    if (req.headers['content-type']) {
      const contentT = contentType.parse({ headers: req.headers });

      if (!ALLOWED_REQUEST_TYPES.includes(contentT.type)) {
        // Throw an error because invalid request type
      }
    }

    next();
  }
}

Create your first Controller

Create the Express Application

Last updated