easy-express-endpoints
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Easy Express Endpoints

A simple and fast way to create API-endpoints for express, where each endpoint is it's own class.

Example

ExampleEndpoint.ts

import { ValidationChain, body } from "express-validator";
import { GetEndpoint, RequestData } from "easy-express-endpoints";

type BodyData {
  id: number;
}

export class ExampleEndpoint extends GetEndpoint {
  constructor() {
    super('/example/endpoint/path');
    this.setAuthRequired(false);
  }

  protected getValidator(): ValidationChain[] {
    return [
      body('id').isInt({ min: 0 }).toInt()
    ]
  }

  protected async execute(data: RequestData<BodyData>): Promise<void> {
    // Handle request here
    return "Hello world!";
  }
}

index.ts

import express, { Router } from 'express';
import http from 'http';

const PORT = 3000;
const app = express();
const server = http.createServer(app);

const apiRouter = Router();
const endpoint = new ExampleEndpoint();
endpoint.setupEndpoint(apiRouter);

app.use('/api', apiRouter);
server.listen(PORT);

Readme

Keywords

none

Package Sidebar

Install

npm i easy-express-endpoints

Weekly Downloads

5

Version

1.1.1

License

ISC

Unpacked Size

58.3 kB

Total Files

23

Last publish

Collaborators

  • gustavps