GithubHelp home page GithubHelp logo

s1njar / skeleton-express Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 37 KB

This project is a boilerplate for creating an expressjs api with ioc container and typescript support. It already has integration of mongoDB and provides routes for authenticating users.

Shell 0.92% TypeScript 99.08%
boilerplate nodejs typescript api ioc-container skeleton express-typescript-boilerplate mongodb-database

skeleton-express's Introduction

Express IOC TS Boilerplate

Introduction

This project is a boilerplate for creating an expressjs api with ioc container and typescript support. It already has integration of mongoDB and provides routes for authenticating users.

It can be used for fast creating and expanding nodejs api's.

Installation

1. Clone project
$ git clone https://github.com/s1njar/express-ioc-ts-boilerplate express-api
2. Change directory to project
$ cd express-api
3. Remove git folder
$ rm -rf .git
4. Copy dotenv config
$ cp .env.example .env 
5. Install package dependencies
$ npm install 
6. Run project
$ npm run watch 
7. After connecting und starting it should look like following:
$ 5:51:51 PM - File change detected. Starting incremental compilation...


$ 5:51:51 PM - Found 0 errors. Watching for file changes.
$ 2020-09-20 17:51:51 [info  project]: Successful connected to MongoDB
$ 2020-09-20 17:51:51 [info  project]: App listening on http://0.0.0.0:5000

Configuration

Configure MongoDB in .env:
APP_HOSTNAME=0.0.0.0
APP_PORT=5000
APP_TOKEN_SECRET=Token123!456$
APP_TOKEN_DURATION=86400
APP_DEBUG_MODE=1

DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=db
DB_USER=
DB_PASSWORD=

Usage

Register new user:
POST: "http://0.0.0.0:5000/user/register"
{
    "email": "[email protected]",
    "password": "Password123"
}
{
    "status": 200,
    "message": "ok",
    "data": {
        "_id": "5f67773619a9f93339c1fb6f",
        "email": "[email protected]",
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmNjc3NzM2MTlhOWY5MzMzOWMxZmI2ZiIsImVtYWlsIjoiczFuamFyLm1haWxAZ21haWwuY29tIiwiaWF0IjoxNjAwNjE2MjQ2LCJleHAiOjE2MDA3MDI2NDZ9.0016EStB_50RgwP552hYAxFc73mcD_vr__ZZoGx3hyU"
    }
}
Login existing user:
POST: "http://0.0.0.0:5000/user/login"
{
    "email": "[email protected]",
    "password": "Password123"
}
{
    "status": 200,
    "message": "ok",
    "data": {
        "_id": "5f6779669908914dd6e580f4",
        "email": "[email protected]",
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmNjc3OTY2OTkwODkxNGRkNmU1ODBmNCIsImVtYWlsIjoiczFuamFyLm1haWxAZ21haWwuY29tIiwiaWF0IjoxNjAwNjE2ODA4LCJleHAiOjE2MDA3MDMyMDh9.CpTlEtbmVvrD1HWl41-kqpv-lkQc5482Ok-HqbgT-_E"
    }
}

Expand project

Create new Controller:

1. Create controller class which extends Controller

In constructor of the controller class you can define routes, which call then defined closures. Auth protected routes can be used with setting middleware 'validateToken', between route path and route action call.

import {Controller} from "../../Framework/Controller/Controller";
import * as express from "express"; 
import validateToken from "./validateToken";

export class ExampleController extends Controller{
    constructor() {
        super();
        this.router.post('/example', this.example);
        this.router.post('/example/auth', validateToken, this.example);
    }

    private example = (
        request: express.Request,
        response: express.Response
    ): void => {
        
    }
}
2. Respond from action

To respond from action its either possible to return data directly via response or to call method responde() with a Promise, which promises IResultResponse:

this.respond(response, Promise<IResultResponse>);
response.status(200).json({id: 1});
{
    "status": 200,
    "message": "ok",
    "data": {
      "id": 1
    }
}

If an exception will be thrown in code it will converted into structured data and respond it.

throw new Exception(500, "Example error message.");
{
    "status": 500,
    "message": "Example error message.",
    "errors": []
}
3. Init controller in server.ts
const controllers = [
    Container.get(UserController),
    Container.get(ExampleController)
];

Create new Service Provider:

1. Create new Service Provider with implementing IServiceProvider

It has to return a custom Promise.

export class ExampleServiceProvider implements IServiceProvider {
    public register(): Promise<void> {
        return new Promise((resolve) => {
            //TODO: Add your code
            resolve();
        })
    }
}
2. Init Service Provider
const serviceProvider = [
    Container.get(MongoDbServiceProvider),
    Container.get(ExampleServiceProvider)
];

Creating middlewares:

1. Create new middleware function.
export default (
    request: Request,
    response: Response,
    next: NextFunction
) => {
    //TODO: Add your code
    next();
}
2. Init middleware

In app class its possible to add middlewares before and after the controllers will be called. This is necessary for loading sequence.

export class App{
    public preInitializeMiddlewares() {
        this.app.use(bodyParser.json());
        this.app.use(morgan('dev'));
        this.app.use(corsMiddleware);
    }

    public postInitializeMiddlewares() {
        this.app.use(errorMiddleware);
    }
}

Using dependency injection:

1. Injecting classes into class.

A detailed description can be found at https://www.npmjs.com/package/typescript-ioc.

export class UserController{
    @Inject
    private registerService: RegisterService;

    @Inject
    private loginService: LoginService;
}

Credits

If you got questions or feedback feel free to contact me.

Links

License

The MIT License (MIT).

skeleton-express's People

Contributors

s1njar avatar deepsourcebot avatar

Stargazers

 avatar Mustafa Akbaş avatar Dome avatar

Watchers

mmubasher avatar  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.