|
1 |
| -# TypeScript Microservice Starter  |
| 1 | +#  Stampo - TypeScript Microservice Starter |
2 | 2 |
|
3 | 3 | [](https://github.com/prettier/prettier)
|
4 | 4 | [](https://eslint.org)
|
5 |
| -[](https://github.com/tapjs/node-tap)   |
| 5 | +[](https://github.com/tapjs/node-tap) |
| 6 | + |
| 7 | + |
6 | 8 |
|
7 |
| -This is an easy boilerplate for building Node.js microservices with TypeScript. Put your typescript code inside `./src` folder and you are ready to go! |
| 9 | +`Stampo` is a friction-free features-complete boilerplate for building Node.js backend services and microservices with TypeScript. It works on Windows, Linux, and macOS and makes the developer productive in no time! It supports any _Active LTS_ Node.js version (`12.12.x`, `14.x.x`, `16.x.x`). |
| 10 | + |
| 11 | +There are only three steps you need to do to be productive after `Stampo` is initialized (follow the [Getting Started](#getting-started) section): |
| 12 | +1. Put your code inside the `./src` folder |
| 13 | +2. Put your tests inside the `./test` folder. |
| 14 | +3. Relax and enjoy coding! |
8 | 15 |
|
9 | 16 | ## Features
|
10 | 17 |
|
11 | 18 | * VS Code debugger configs in .vscode folder
|
12 |
| -* recommended Dockerfile for secure nodejs container |
13 |
| -* tsconfig.json for [typescript compiler](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) |
| 19 | +* recommended Dockerfile for secure Node.js production-ready images |
| 20 | +* most strict and backend specific [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) configuration |
| 21 | +* configured tests and reporters via [tap](https://node-tap.org) |
14 | 22 |
|
15 | 23 | ## Getting Started
|
| 24 | +### Clone the repo |
| 25 | +``` |
| 26 | +$ git clone https://github.com/nucleode/typescript-microservice-starter.git {your_project_name} |
| 27 | +$ cd {your_project_name} |
| 28 | +``` |
16 | 29 |
|
| 30 | +### Remove references to the original starter |
| 31 | +``` |
| 32 | +$ rm -rf .git && npm init |
17 | 33 | ```
|
18 |
| -# Clone the repo |
19 |
| -git clone https://github.com/nucleode/typescript-microservice-starter.git {your_project_name} |
20 |
| -cd {your_project_name} |
21 | 34 |
|
22 |
| -# Remove reference to the original starter |
23 |
| -rm -rf .git && npm init |
| 35 | +### Initialize a git repository with your own |
| 36 | +``` |
| 37 | +$ git init |
| 38 | +``` |
24 | 39 |
|
25 |
| -# Initialize git repo with your own |
26 |
| -git init |
| 40 | +### Install development dependencies |
| 41 | +``` |
| 42 | +$ npm i |
| 43 | +``` |
27 | 44 |
|
28 |
| -# Install development dependencies |
29 |
| -npm i |
| 45 | +### Add remote origin and make an initial commit |
| 46 | +``` |
| 47 | +$ git remote add origin [email protected]:{your_repository}.git |
| 48 | +$ git add . |
| 49 | +$ git commit -m "Initial commit" |
| 50 | +$ git push -u origin master |
| 51 | +``` |
| 52 | +### Start the development server |
30 | 53 |
|
31 |
| -# Add remote origin and make initial commit |
| 54 | +``` |
| 55 | +$ npm run dev |
| 56 | +``` |
| 57 | + |
| 58 | +## Included npm scripts |
| 59 | +`Stampo` includes a bunch of scripts that cover the most common scenarios for Node.js backend projects. |
32 | 60 |
|
33 |
| -git remote add origin [email protected]:{your_repository}.git |
34 |
| -git add . |
35 |
| -git commit -m "Initial commit" |
36 |
| -git push -u origin master |
| 61 | +The commands must be run from the project's root folder. |
37 | 62 |
|
38 |
| -# start development server |
39 |
| -npm run dev |
| 63 | +### `dev` |
| 64 | +It runs the project in development mode. It uses [`nodemon`](https://github.com/remy/nodemon) to watch the `./src/**/*.ts` files and restart the server on save. It exposes the debugger port, ready to be used via the provided VS Code configuration. |
| 65 | +``` |
| 66 | +$ npm run dev |
40 | 67 | ```
|
41 | 68 |
|
42 |
| -## Included npm scripts |
| 69 | +### `build` |
| 70 | +It builds for production all files from the `./src` to the `./build` folder. It uses `tsc` directly and therefore checks types too. It also emits the source maps. |
| 71 | +``` |
| 72 | +$ npm run build |
| 73 | +``` |
| 74 | + |
| 75 | +### `start` |
| 76 | +It runs previously built code from the `./build` folder. In addition, it uses `--enable-source-maps` flag for native source-maps support. Note: this flag is present in Node.js since version `12.12.x`. |
| 77 | +``` |
| 78 | +$ npm run start |
| 79 | +``` |
| 80 | +It is advised to run Node.js binary directly to avoid any overhead or `sigterm` propagation issues in production. |
| 81 | +``` |
| 82 | +$ node --enable-source-maps build/index.js |
| 83 | +``` |
43 | 84 |
|
44 |
| -Run this commands from the project folder with `npm run "script-name"`. |
45 |
| -* `dev`: runs project in development mode, with [ts linter](https://palantir.github.io/tslint/) and `chokidar` watching `.ts` files inside `./src` folder and autorestart on save. |
46 |
| -* `build`: builds all .ts files from `./src` folder to `./build` |
47 |
| -* `lint`: lints source code using `tslint` |
48 |
| -* `update`: easily check for updates and update all dependencies |
49 |
| -* `test`: run tests using tap |
50 |
| -* `test:watch`: run tests using tap in watch mode |
51 |
| -* `test:report`: run tests using tap and adds report file |
52 |
| -* `test:reporter`: run tests using tap and convert output to mocha |
| 85 | +### `lint` |
| 86 | +It uses [`eslint`](https://eslint.org) and [`prettier`](https://prettier.io) to lint the code. It checks `./src` and `./test` folders. Note: `prettier` is run as `eslint` plugin via [`eslint-plugin-prettier`](https://github.com/prettier/eslint-plugin-prettier). |
| 87 | +``` |
| 88 | +$ npm run lint |
| 89 | +``` |
| 90 | +If you want to fix all of the fixable problems, run |
| 91 | +``` |
| 92 | +$ npm run lint -- --fix |
| 93 | +``` |
| 94 | + |
| 95 | +### `update` |
| 96 | +It uses [npm-check](https://www.npmjs.com/package/npm-check) to help you upgrading your dependencies and never have any outdated and broken packages again. |
| 97 | +``` |
| 98 | +$ npm run update |
| 99 | +``` |
| 100 | + |
| 101 | +### `test` |
| 102 | +It uses [`tap`](https://node-tap.org) to run tests. Since version 15 `tap` needs `ts-node` to run TS files, `Stampo` also includes it. |
| 103 | +``` |
| 104 | +$ npm run test |
| 105 | +``` |
| 106 | + |
| 107 | +### `test:watch` |
| 108 | +It runs `tap` in [watch mode](https://node-tap.org/docs/watch/) with interactive repl. |
| 109 | +``` |
| 110 | +$ npm run test:watch |
| 111 | +``` |
| 112 | + |
| 113 | +### `test:report` |
| 114 | +It runs tests and reports the results in the widely used `junit` format using [`tap-mocha-reporter`](https://www.npmjs.com/package/tap-mocha-reporter). The default `xunit` reporter can be changed to anyone from the [supported reporters list](https://node-tap.org/docs/reporting/). This command is mainly intended to be used in CI/CD environments. The generated `junit-testresults.xml` can be consumed by automatic reporting systems. |
53 | 115 |
|
54 | 116 | ## External typings augmentation
|
55 |
| -This starter is already configured to allow you to extend typings of external packages. The logic behind it is based on [this](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html) official template. To augment a module, just create a folder with the same name as the module you are augmenting and add an index.d.ts in it. [Here](https://github.com/fox1t/fastify-websocket-router/tree/master/typings/fastify) you can find a real world example. |
| 117 | +`Stampo` is configured to allow you to extend typings of external packages using `./typings` folder. The logic behind it is based on [this](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html) official template. To augment a module, create a folder with the same module name you are augmenting and add an `index.d.ts` file inside it. [Here](https://github.com/fox1t/fastify-websocket-router/tree/master/typings/fastify) you can find a real-world example. |
56 | 118 |
|
57 |
| -## Debugging |
58 |
| -> Warning: This starter uses new V8 [inspect protocol](https://nodejs.org/api/debugger.html) so you have to use at least Node.js 7.7.4 if you want to use the included debugger settings. |
| 119 | +## Debugging Steps |
| 120 | + |
| 121 | +* run the `dev` script to start your application (`$ npm run dev`) |
| 122 | +* either |
| 123 | + * use the VS Code included `attach` config for the best debugging experience |
| 124 | + <img width="327" alt="image" src="https://user-images.githubusercontent.com/1620916/129894966-15385c33-da0c-4e00-9f6f-a8ddf966e63e.png"> |
59 | 125 |
|
60 |
| -#### Steps: |
61 |
| -* start dev server with `npm run dev` |
62 |
| -* now you have two ways: |
63 | 126 | * use the provided debug URL in Chrome
|
64 |
| - * use VS Code included (inside .vscode folder) `attach` config (best debugging experience) |
65 | 127 |
|
66 | 128 | ## Docker Support
|
67 | 129 |
|
68 |
| -This stater uses Node.js best practices and creates dummy user to start node process, instead of using root user. |
| 130 | +`Stampo` provides a `Dockerfile` that follows the best practices regarding Node.js containerized applications. |
| 131 | +* the application is run using a dedicated non-root user |
| 132 | +* the Dockerfile uses a dedicated build step |
| 133 | + |
69 | 134 |
|
| 135 | +### Build your docker image |
70 | 136 | ```
|
71 |
| -# Go to the root of your repo created from this starter |
72 |
| -# Build your docker image |
73 | 137 | docker build -t my-project-name .
|
| 138 | +``` |
74 | 139 |
|
75 |
| -# run your docker container |
| 140 | +### Run your docker container |
| 141 | + |
| 142 | +``` |
76 | 143 | docker run -p PORT:PORT my-project-name
|
77 | 144 | ```
|
0 commit comments