|
| 1 | +# Express Typescript Boilerplate |
| 2 | + |
| 3 | +[](https://david-dm.org/w3tecch/express-typescript-boilerplate) |
| 4 | +[](https://travis-ci.org/w3tecch/express-typescript-boilerplate) |
| 5 | +[](https://ci.appveyor.com/project/dweber019/express-typescript-boilerplate/branch/master) |
| 6 | + |
| 7 | +> A delightful way to building a RESTful API with NodeJs & TypeScript. |
| 8 | +> An Node.js Web-Serice boilerplate/skeleton/starter-kit featuring |
| 9 | +[TypeScript](https://www.typescriptlang.org/), |
| 10 | +[Express](https://expressjs.com/), |
| 11 | +[Winston](https://github.com/winstonjs/winston), |
| 12 | +[Microframework](https://github.com/pleerock/microframework), |
| 13 | +[TypeDI](https://github.com/pleerock/typedi), |
| 14 | +[TypeORM](https://github.com/typeorm/typeorm), |
| 15 | +[TsLint](http://palantir.github.io/tslint/), |
| 16 | +[@types](https://www.npmjs.com/~types), |
| 17 | +[Jest](https://facebook.github.io/jest/), |
| 18 | +[Swagger](http://swagger.io/), |
| 19 | +[validatejs](https://validatejs.org/), |
| 20 | +by [w3tech](https://github.com/w3tecch) |
| 21 | + |
| 22 | +## Why |
| 23 | + |
| 24 | +Our main goal with this project is a feature complete server application. |
| 25 | +We like you to be focused on your business and not spending hours in project configuration. |
| 26 | + |
| 27 | +Try it!! We are happy to hear your feedback or any kind of new features. |
| 28 | + |
| 29 | +## Features |
| 30 | + |
| 31 | +- **Beautiful Code** thanks to the awesome annotations of the libraries from [pleerock](https://github.com/pleerock). |
| 32 | +- **Easy API Testing** with included e2e testing. |
| 33 | +- **Dependency Injection** done with the nice framework from [TypeDI](https://github.com/pleerock/typedi). |
| 34 | +- **Simplified Database Query** with the ORM [TypeOrm](https://github.com/typeorm/typeorm). |
| 35 | +- **Clear Structure** with different layers such as controllers, services, repositories, models, middlewares... |
| 36 | +- **Easy Exception Handling** thanks to [routing-controllers](https://github.com/pleerock/routing-controllers). |
| 37 | +- **Smart Validation** thanks to [class-validator](https://github.com/pleerock/class-validator) with some nice annotations. |
| 38 | +- **Custom Validators** to validate your request even better and stricter. [custom-validation-classes](https://github.com/pleerock/class-validator#custom-validation-classes) |
| 39 | +- **API Documentation** thanks to [swagger](http://swagger.io/). |
| 40 | +- **API Monitoring** thanks to [express-status-monitor](https://github.com/RafalWilinski/express-status-monitor). |
| 41 | +- **Integrated Testing Tool** thanks to [Wallaby.js](https://wallabyjs.com/) |
| 42 | +- **Basic Security Features** thanks to [Helmet](https://helmetjs.github.io/) |
| 43 | + |
| 44 | +### Comming soon |
| 45 | + |
| 46 | +- **Fast Database Building** with simple migration and seeding from [Knex](http://knexjs.org/). |
| 47 | +- **Easy Data Seeding** with our own factories. |
| 48 | +- **Custom Commands** are also available in our setup and really easy to use or even extend. |
| 49 | +- **Scaffolding Commands** will speed up your development tremendously as you should focus on business code and not scaffolding. |
| 50 | + |
| 51 | +## Getting Started |
| 52 | + |
| 53 | +### Step 1: Set up the Development Environment |
| 54 | + |
| 55 | +You need to set up your development environment before you can do anything. |
| 56 | + |
| 57 | +Install [Node.js and NPM](https://nodejs.org/en/download/) |
| 58 | + |
| 59 | +- on OSX use [homebrew](http://brew.sh) `brew install node` |
| 60 | +- on Windows use [chocolatey](https://chocolatey.org/) `choco install nodejs` |
| 61 | + |
| 62 | +Install yarn globally |
| 63 | + |
| 64 | +```bash |
| 65 | +npm install yarn -g |
| 66 | +``` |
| 67 | + |
| 68 | +Install a MySQL database. |
| 69 | + |
| 70 | +> If you work with a mac, we recommend to use homebrew for the installation. |
| 71 | +
|
| 72 | +### Step 2: Create new Project |
| 73 | + |
| 74 | +Fork or download this project. Configure your package.json for your new project. |
| 75 | + |
| 76 | +Then copy the `.env.example` file and rename it to `.env`. In this file you have to add your database connection information. |
| 77 | + |
| 78 | +Create a new database with the name you have in your `.env`-file. |
| 79 | + |
| 80 | +Then setup your application environment. |
| 81 | + |
| 82 | +```bash |
| 83 | +nps setup |
| 84 | +``` |
| 85 | + |
| 86 | +> This installs all dependencies with yarn. After that it migrates the database and seeds some test data into it. So after that your development environment is ready to use. |
| 87 | +
|
| 88 | +### Step 3: Serve your App |
| 89 | + |
| 90 | +Go to the project dir and start your app with this npm script. |
| 91 | + |
| 92 | +```bash |
| 93 | +nps serve |
| 94 | +``` |
| 95 | + |
| 96 | +> This starts a local server using `nodemon`, which will watch for any file changes and will restart the sever according to these changes. |
| 97 | +> The server address will be displayed to you as `http://0.0.0.0:3000`. |
| 98 | +
|
| 99 | +## Scripts / Tasks |
| 100 | + |
| 101 | +All script are defined in the package.json file, but the most important ones are listed here. |
| 102 | + |
| 103 | +### Install |
| 104 | + |
| 105 | +- Install all dependencies with `yarn install` |
| 106 | + |
| 107 | +### Linting |
| 108 | + |
| 109 | +- Run code quality analysis using `nps lint`. This runs tslint. |
| 110 | +- There is also a vscode task for this called `lint`. |
| 111 | + |
| 112 | +### Tests |
| 113 | + |
| 114 | +- Run the unit tests using `nps test` (There is also a vscode task for this called `test`). |
| 115 | +- Run the e2e tests using `nps test:e2e` and don't forget to start your application and your [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server). |
| 116 | + |
| 117 | +### Running in dev mode |
| 118 | + |
| 119 | +- Run `nps serve` to start nodemon with ts-node, to serve the app. |
| 120 | +- The server address will be displayed to you as `http://0.0.0.0:3000` |
| 121 | + |
| 122 | +### Building the project and run it |
| 123 | + |
| 124 | +- Run `nps build` to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called `build`). |
| 125 | +- To start the builded app located in `dist` use `npm start`. |
| 126 | + |
| 127 | +## Using the debugger in VS Code |
| 128 | + |
| 129 | +Just set a breakpoint and hit `F5` in your Visual Studio Code. |
| 130 | + |
| 131 | +## API Routes |
| 132 | + |
| 133 | +The route prefix is `/api` by default, but you can change this in the .env file. |
| 134 | +The swagger and the monitor route can be altered in the `.env` file. |
| 135 | + |
| 136 | +| Route | Description | |
| 137 | +| ------------ | ----------- | |
| 138 | +| **/api** | Shows us the name, description and the version of the package.json | |
| 139 | +| **/swagger** | This is the Swagger UI with our API documentation | |
| 140 | +| **/monitor** | Shows a small monitor page for the server | |
| 141 | + |
| 142 | +## Project Structure |
| 143 | + |
| 144 | +| Name | Description | |
| 145 | +| ----------------------------- | ----------- | |
| 146 | +| **.vscode/** | VSCode tasks, launch configuration and some other settings | |
| 147 | +| **dist/** | Compiled source files will be placed here | |
| 148 | +| **src/** | Source files | |
| 149 | +| **src/api/controllers/** | REST API Controllers | |
| 150 | +| **src/api/errors/** | Custom HttpErrors like 404 NotFound | |
| 151 | +| **src/api/interceptors/** | Interceptors are used to change or replace the data returned to the client. | |
| 152 | +| **src/api/middlewares/** | Express Middlewares like helmet security features | |
| 153 | +| **src/api/models/** | Bookshelf Models | |
| 154 | +| **src/api/repositories/** | Repository / DB layer | |
| 155 | +| **src/api/services/** | Service layer | |
| 156 | +| **src/api/validators/** | Custom validators, which can be used in the request classes | |
| 157 | +| **src/api/** swagger.json | Swagger documentation | |
| 158 | +| **src/console/** | Command line scripts | |
| 159 | +| **src/constants/** | Global Constants | |
| 160 | +| **src/core/** | The core features like logger and env variables | |
| 161 | +| **src/public/** | Static assets (fonts, css, js, img). | |
| 162 | +| **src/types/** *.d.ts | Custom type definitions and files that aren't on DefinitelyTyped | |
| 163 | +| **test** | Tests | |
| 164 | +| **test/e2e/** *.test.ts | End-2-End tests (like e2e) | |
| 165 | +| **test/unit/** *.test.ts | Unit tests | |
| 166 | +| .env.example | Environment configurations | |
| 167 | + |
| 168 | +## Related Projects |
| 169 | + |
| 170 | +- [Microsoft/TypeScript-Node-Starter](https://github.com/Microsoft/TypeScript-Node-Starter) - A starter template for TypeScript and Node with a detailed README describing how to use the two together. |
| 171 | +- [express-graphql-typescript-boilerplate](https://github.com/w3tecch/express-graphql-typescript-boilerplate) - A starter kit for building amazing GraphQL API's with TypeScript and express by @w3tecch |
| 172 | +- [aurelia-typescript-boilerplate](https://github.com/w3tecch/aurelia-typescript-boilerplate) - An Aurelia starter kit with TypeScript |
| 173 | +- [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server) - Useful for e2e testing or faking an oAuth server |
| 174 | + |
| 175 | +## Documentations of our main dependencies |
| 176 | + |
| 177 | +| Name & Link | Description | |
| 178 | +| --------------------------------- | --------------------------------- | |
| 179 | +| [Express](https://expressjs.com/) | Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. | |
| 180 | +| [Microframework](https://github.com/pleerock/microframework) | Microframework is a simple tool that allows you to execute your modules in a proper order, helping you to organize bootstrap code in your application. | |
| 181 | +| [TypeDI](https://github.com/pleerock/typedi) | Dependency Injection for TypeScript. | |
| 182 | +| [routing-controllers](https://github.com/pleerock/routing-controllers) | Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework. | |
| 183 | +| [TypeORM](http://typeorm.io/#/) | TypeORM is highly influenced by other ORMs, such as Hibernate, Doctrine and Entity Framework. | |
| 184 | +| [class-validator](https://github.com/pleerock/class-validator) | Validation made easy using TypeScript decorators. | |
| 185 | +| [class-transformer](https://github.com/pleerock/class-transformer) | Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors | |
| 186 | +| [event-dispatcher](https://github.com/pleerock/event-dispatch) | Dispatching and listening for application events in Typescript | |
| 187 | +| [Helmet](https://helmetjs.github.io/) | Helmet helps you secure your Express apps by setting various HTTP headers. It’s not a silver bullet, but it can help! | |
| 188 | +| [Auth0 API Documentation](https://auth0.com/docs/api/management/v2) | Authentification service | |
| 189 | +| [Jest](http://facebook.github.io/jest/) | Delightful JavaScript Testing Library for unit and e2e tests | |
| 190 | +| [swagger Documentation](http://swagger.io/) | API Tool to describe and document your api. | |
| 191 | + |
| 192 | +## License |
| 193 | + |
| 194 | +[MIT](/LICENSE) |
| 195 | + |
| 196 | +--- |
| 197 | +Made with ♥ by w3tech ([w3tech](https://github.com/w3tecch)), Gery Hirschfeld ([@GeryHirschfeld1](https://twitter.com/GeryHirschfeld1)) and [contributors](https://github.com/w3tecch/express-typescript-boilerplate/graphs/contributors) |
0 commit comments