Giter Club home page Giter Club logo

simple-express-ts's Introduction

simple-express-ts

Status Branch
Master CircleCI
Dev CircleCI

simple-express-ts is a wrapper for express with typescript decorators, and that's all, no additional dependencies.

I wrote this to avoid using huge frameworks that allow to do the same while having billions features that sometimes I don't need at all.

Install

simple-express-ts wraps express and its types, so you only need to install this to have

  • Express
  • Express types
  • Express typescript decorators

Install with npm

npm install simple-express-ts

or with yarn

yarn add simple-express-ts

Usage

Controller class:

import { ExpressController, Get } from 'simple-express-ts';
import { Request, Response } from 'express';

@ExpressController('/controller')
export class Controller {

    @Get('/test')
    private async testRoute(req: Request, res: Response){
        res.sendStatus(200);
    }

}

Index file:

import { ExpressServer } from 'simple-express-ts';
import { Controller } from './controller';

const server = new ExpressServer({
    port: 7777,
    controllers: [Controller]
});
server.start();

Running your compiled index at this point, you should be able to obtain an OK response on http://localhost:3000/controller/test

Using middlewares on server

You can specify middlewares while building express server

import { ExpressServer } from 'simple-express-ts';
import { Controller } from './controller';
import bodyParser = require('body-parser');

const server = new ExpressServer({
    port: 3000,
    controllers: [Controller],
    middlewares: [bodyParser.json(), bodyParser.urlencoded({ extended: true })]
});
server.start();

Using middlewares on controllers

import { ExpressController, ExpressControllerMiddleware, Get } from 'simple-express-ts';
import { Request, Response, NextFunction } from 'express';

@ExpressController('/controller')
@ExpressControllerMiddleware((req: Request, res: Response, next: NextFunction) => {
    if (req.query.code !== '12345') res.sendStatus(401);
    else next();
})
export class Controller {

    @Get('/secret')
    private getSecret(req: Request, res: Response){
        res.send('Secret!');
    }

}

Using middlewares on routes

import { ExpressController, Get, Middleware } from 'simple-express-ts';
import { Request, Response, NextFunction } from 'express';

@ExpressController('/controller')
export class Controller {

    @Middleware((req: Request, res: Response, next: NextFunction) => {
        req.params.value = 'Ulp!';
        next();
    })
    @Get('/testMiddleware')
    private async testMiddleware(req: Request, res: Response){
        const { value } = req.params;
        res.send({ value }); // Ulp!
    }

}

Using HTTPS

import { ExpressServer } from 'simple-express-ts';
import { Controller } from './controller';

const server = new ExpressServer({
    httpPort: 3000,
    httpsPort: 3001,
    controllers: [Controller],
    httpsOptions: {
        cert: 'Your certificate here',
        key: 'Your key here'
    }
});
server.start();

simple-express-ts's People

Contributors

dependabot[bot] avatar tiziofittizio avatar

Watchers

 avatar

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.