Skip to content

adilajjemami/express-typescript-boilerplate

Repository files navigation

Express Typescript Boilerplate

Build Status codecov

Table of Contents

Installation

git clone https://github.com/adilajjemami/express-typescript-boilerplate.git
cd express-typescript-boilerplate
yarn install

Long live to Expressjs, Typescript and Yarn 🤘

Introduction

Like most starter-kits and boilerplate projects, this project has a simple philosophy in other words : Be simple !

Project Structure

project 
│
└───src
│   │   Server.ts
│   │   index.ts
│   │
│   └───Controller
│   |   │   BaseController.ts // Abstr  ct controller
│   |   │   HelloController.ts // Controller example
|   |
│   └───Core
│   |   │   Core.ts // Application Core
│   |   │   DependencyInjection.ts // Injection / import utility
|   |
│   └───Middlewares
│   |   │   ErrorMiddleware.ts // Expressjs middleware that return json response when an exception is thrown
|   |
│   └───Services
│   |   │   FakeService.ts // Service example
│   |   │   HelloService.ts // Service example
|   |
│   └───Utils
│   |   │   ApiError.ts // Custom Error class
│   |   │   GlobalMessage.ts // Get error object based on an errorCode
|   |   
│   └───config
│       │   config.ts // Global/shared configuration
│       │   configDev.ts // Development configuration. NODE_ENV=dev
│       │   configDev.ts // Production configuration. NODE_ENV=prod
│       │   configTest.ts // Production configuration. NODE_ENV=test
│       │   middlewares.ts // Middlewares configuration
│       │   parameters.ts // Application parameters
│       │   routing.ts // Apllication routing
│       │   services.ts // Services configuration
│   
└───Resources
|   │   errors.json // Global error messages
|
└───test // Unitests
└───dist // Dist files

Server

import http from 'http';
import { Server } from './Server';

Server.boostrap()
    .then((server) => {
        // Create server
        const httpServer = http.createServer(app);

        // Start listening on port 3000
        // You can override port by using NODE_PORT=8080
        httpServer.listen(server.getPort());
    });

If you prefer async/await:

import http from 'http';
import { Server } from './Server';

...

const server = await Server.bootstrap();

// Create server
const httpServer = http.createServer(app);

// Start listening on port 3000
// You can override port by using NODE_PORT=8080
httpServer.listen(server.getPort());

NPM Scripts

Code Linting

To lint the source code (tsLint):

npm run lint

Code Testing

To test the source code (mocha/chai):

npm run test

Check Security

To dependencies check security issues (retirejs):

npm run security-checker

Build Project

To build the project:

npm run build

Serve Dev

You can run the server (dev):

npm run serve-dev