1.1.0 • Published 1 year ago

@docsploit/espress v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Express JS Boilerplate

Customized express.js application with additional functions

Installation

To install and set up the library, run:

$ npm install -S @docsploit/espress

Usage

  • main.ts
import { Server } from '@docsploit/espress';

const server = new Server('Project Name',<options>);

server.run();
  • module.controller.ts
import { Controller, Req, Res, GET, POST } from '@docsploit/router';

import AuthModel from './auth.model';

const authSchema={
    type: 'object',
    required: ['passowrd', 'username'],
    properties: {
      passowrd: {
        type: 'string',
      },
      username: {
        type: 'string',
      },
    },
  }
/**
 *
 * @name Auth
 * 
 * @desc Authentication module
 * 
 *  
 */
@Controller('/auth')
export default class Auth {
  /**
    * @name Login
    * 
    * @desc Login users using username and password
    * 
    * @response 500="Internal Server Error",400="Bad Request",455="Bad Gateway"
    * 
    */
  @POST<AuthModel>('/',{schema:authSchema,middleware:[]})
  async login(req: Req, res: Res) {...}

}

Server by default runs at port 5000 if it is not provided.

API

class Server

const server = new Server({}, modules, 'Project');

Main class. express instance is initialized when it is constructed.

Params

parametertypeusagerequireddefault
namestringProject Nametrue-
propsServerOptionsTo set CORS and JSON optionsfalsedefault CORS and JSON options

properties

For deep handling Server instance initialize properties express and app.

const server = new Server({}, modules, 'Project');
server.express; // express module
server.app; // app= express()
server.run(); // TO run server

decorator Controller

@Controller(path)
class User {}

Controller decorator is needed, in order to work the method decorators. Every modules should be decorated using Controller which has a parameter path which act as base route

decorator Get,Post,Put,Delete,Patch

@Controller('/user')
class User{
    @GET('/all')
    getAll(req:Req,res:Res){

    }
}

In this example the generated route will be /api/<version>/user/all

Params

parametertypeusagerequireddefault
pathstringrouter pathtrue-
optionsRouteOptionsmiddleware and schemafalseundefined

Note Schema validations do not work with FormData requests due to incompatibility

method register()

This function register the modules to express application.

import { Server } from '@docsploit/espress/core';
import ExampleController from './app/Example/Example.controller';


const server = new Server('Example', {});

server.register(ExampleController)

server.run({
    apiDocs:true,
    versioning: true,
    version: 'v1',
    port: 3000
})

Params

parametertypeusagerequireddefault
controllerClassControllertrue-

- Utilities

method sendErrorResponse() and sendSuccessResponse

import { sendErrorResponse, sendSuccessResponse } from '@docsploit/espress/utils';

if (true) {
  return sendSuccessResponse('success', data, res);
} else {
  return sendErrorResponse(400, 'Invalid Data', res);
}

These functions are the wrappers for different responses.

Params

parametertypeusagerequireddefault
status (Error)numbersend status code to clienttrue-
messagestringresponse messagetrue-
data (Success)anyresponse datafalseundefined
resResponseexpress response Objecttrue-

- File Handler

Predefined multer middleware for handling files in indigenous way

method multerSingleHandler () and multerMultiFieldHandler()

@GET('/',{middleware:[multerSingleField('profile','static/profiles','image/jpeg')]})
uploadImage(req:Req,res:res){
    const image = req.body.profile;
    ...

Params

parametertypeusagerequireddefault
keyNamestringrequest-body identifiertrue-
pathstringfile storing pathtrue-
mimeTypestring[]file typetrue-

method validate()

    import {validate} from '@docsploit/espress/schemaValidator'
    ...
    const data = req.body;
    const schema ={...}
    const valid = validate(schema,req.body)

AJV validator wrapper. By default method decorator have this validation functions.But that will work only if the schema is provided.

Params

parametertypeusagerequireddefault
schemaJSONSchemaTypeajv schematrue-
bodyObjectrequest datatrue-

Static file

To make a folder static add its path in .espressive to enable them

{
  "framework": {
    "version": "1.0.16",
    "assets": [
      "/static"
      // add new path
    ]
  }
}

by default /static folder and 404.html file is created when initializing project with espress.

  • Files in /static folder can be access through root path
  • rest of the static folders can be accessed through the same name passed in .espress file.

API Documentation

Authors

See also the list of contributors who participated in this project.

1.0.19

1 year ago

1.1.0

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.15

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago