Skip to content

yehjunying/express-typescript-boilerplate

Repository files navigation

Express Typescript Boilerplate

Dependency Status Build Status

A delightful way to building a RESTful API with NodeJs & TypeScript.

An Node.js RESTful API boilerplate featuring Express, Inversify, Winston, TypeScript, TsLint, @types, Jest, Swagger, validatejs, knex and bookshelf by w3tech

Why

Our main goal with this project is a feature complete server application. We like you to be focused on your business and not spending hours in project configuration.

Try it!! We are happy to hear your feedback or any kind of new features.

Features

  • Beautiful Syntax thanks to the awesome annotations from Inversify Express Utils.
  • Easy API Testing with included black-box testing.
  • Dependency Injection done with the nice framework from Inversify.
  • Fast Database Building with simple migration and seeding from Knex.
  • Simplified Database Query with the ORM of Knex called Bookshelf.
  • Clear Structure with controllers, services, repositories, models, middlewares...
  • Easy Exception Handling with our own simple and easy to adopt logic. You will love it.
  • Easy Data Seeding with our own factories.
  • Custom Commands are also available in our setup and really easy to use or even extend.
  • Scaffolding Commands will speed up your development tremendously as you should focus on business code and not scaffolding.
  • Smart Validation thanks to class-validator with some nice annotations.
  • API Documentation thanks to swagger.
  • API Monitoring thanks to express-status-monitor.
  • Integrated Testing Tool thanks to Wallaby.js

Getting Started

Prerequisites

  • Install Node.js
  • Install yarn globally npm install yarn -g

Installing

  • fork this repo
  • clone your fork
  • cp .env.example .env to copy the example .env file and enter your database connection
  • Create a new database. You will find the name in the .env file.
  • Run npm run setup or enter the following commands manually:
    • yarn install to install all dependencies and typings.
    • npm run db:migrate to create the schema.
    • npm run db:seed to insert some test data.
  • npm run serve to start the application.

Running the app

After you have installed all dependencies you can run the app. Enter npm run serve to start a local server using nodemon, which will watch for any file changes and will restart the sever according to these changes. The server address will be displayed to you as http://0.0.0.0:3000.

Scripts / Tasks

All script are defined in the package.json file, but the most important ones are listed here.

Install

  • Install all dependencies with yarn install

Linting

  • Run code quality analysis using npm run lint. This runs tslint.
  • There is also a vscode task for this called lint.

Tests

  • Run the unit tests using npm test (There is also a vscode task for this called test).
  • Run the black-box tests using npm run test:black-box and don't forget to start your application and your Auth0 Mock Server.

Running in dev mode

  • Run npm run serve to start nodemon with ts-node, to serve the app.
  • The server address will be displayed to you as http://0.0.0.0:3000

Building the project and run it

  • Run npm run build to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called build).
  • To start the builded app located in dist use npm start.

Database

  • Run npm run db:migrate to migrate schema changes to the database
  • Run npm run db:migrate:rollback to rollback one migration
  • Run npm run db:seed to seed sample data into the database
  • Run npm run db:reset to rollback all migrations and migrate any migration again

Console

  • To run your own created command enter npm run console <command-name>.
  • This list all your created commands npm run console:help.

Scaffolding Commands

All the templates for the commands are located in src/console/templates.

  • npm run console make:resource <file> - Generates a controller, service, requests, repo, model and a migration with CRUD operations.
  • npm run console make:controller <file> - Generates a controller.
  • npm run console make:service <file> - Generates a service.
  • npm run console make:repo <file> - Generates a repository.
  • npm run console make:model <file> - Generates a model with the props and configurations.
  • npm run console make:middleware <file> - Generates a basic middleware.
  • npm run console make:request <file> - Generates a basic request.
  • npm run console make:listener <file> - Generates a basic listener.
  • npm run console make:exception <file> - Generates a basic exception.
  • npm run console update:targets <file> - Reads all the API files and generate a new constants/Targets.ts file out of it.

Example

$ npm run console make:controller auth/auth
// -> creates `api/controllers/auth/AuthController.ts

$ npm run console make:model user
// -> creates `api/models/User.ts

IoC

Our IoC automatically looks through the controllers, listeners , middlewares, services, repositories and models folders in src/api/ for files to bound automatically into the IoC - Container, so you have nothing to do.

However it is very important to keep the naming right, because otherwise our IoC will not find your created files!!

Using the debugger in VS Code

Just set a breakpoint and hit F5 in your Visual Studio Code.

API Routes

The route prefix is /api by default, but you can change this in the .env file.

Route Description
/api/info Shows us the name, description and the version of the package.json
/api/docs This is the Swagger UI with our API documentation
/status Shows a small monitor page for the server

Project Structure

Name Description
.vscode/ VSCode tasks, launch configuration and some other settings
dist/ Compiled source files will be placed here
src/ Source files
src/api/controllers/ REST API Controllers
src/api/exceptions/ Exceptions like 404 NotFound
src/api/listeners/ Event listeners
src/api/middlewares/ Express Middlewares like populateUser
src/api/models/ Bookshelf Models
src/api/repositories/ Repository / DB layer
src/api/requests/ Request bodys with validations
src/api/services/ Service layer
src/api/ swagger.json Swagger documentation
src/console/ Command line scripts
src/config/ Configurations like database or logger
src/constants/ Global Constants
src/core/ The core framework
src/database/factories/ Model factories to generate database records
src/database/migrations/ Migrations scripts to build up the database schema
src/database/seeds/ Seed scripts to fake sample data into the database
src/public/ Static assets (fonts, css, js, img).
src/types/ *.d.ts Custom type definitions and files that aren't on DefinitelyTyped
test Tests
test/black-box/ *.test.ts Black-Box tests (like e2e)
test/unit/ *.test.ts Unit tests
.env.example Environment configurations
knexfile.ts This file is used for the migrations and seed task of knex

Related Projects

Documentations of our main dependencies

License

MIT


Made with ♥ by w3tech (w3tech), Gery Hirschfeld (@GeryHirschfeld1) and contributors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published