Skip to content

Commit dfa9662

Browse files
committed
Updating README
1 parent 3cda884 commit dfa9662

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ The main purpose of this repository is to build a good project setup and workflo
99

1010
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and provides an elegant suite of methods that make writing servers fast and enjoyable.
1111

12-
This boilerplate is deploying in [Heroku](https://koa-typescript-boilerplate.herokuapp.com/)!
12+
Through a Travis-Heroku CI pipeline, this boilerplate is deployed [here](https://node-typescript-koa-rest.herokuapp.com/)! You can try to make requests to the different endpoints defined (GET /, GET /jwt, GET /users, POST /user...) and see how it works. The following Authorization header will have to be set (already signed with the boilerplate's secret) to pass the JWT middleware:
13+
14+
```
15+
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJuYW1lIjoiSmF2aWVyIEF2aWxlcyIsImVtYWlsIjoiYXZpbGVzbG9wZXouamF2aWVyQGdtYWlsLmNvbSJ9.7oxEVGy4VEtaDQyLiuoDvzdO0AyrNrJ_s9NU3vko5-k
16+
```
1317

1418
## Pre-reqs
1519
To build and run this app locally you will need:
@@ -21,6 +25,7 @@ To build and run this app locally you will need:
2125
* TypeORM (SQL DB) with basic CRUD included
2226
* Class-validator - Decorator based entities validation
2327
* Docker-compose ready to go
28+
* Travis CI - Heroku deployment prepared
2429

2530
## Included middleware:
2631
* koa-router
@@ -39,7 +44,12 @@ git clone --depth=1 https://github.com/javieraviles/node-typescript-koa-rest.git
3944
cd <project_name>
4045
npm install
4146
```
42-
- Build and run the project
47+
- Run the project directly in TS
48+
```
49+
npm run watch-server
50+
```
51+
52+
- Build and run the project in JS
4353
```
4454
npm run build
4555
npm run start
@@ -55,9 +65,10 @@ If you use Docker natively, the host for the server which you will need to inclu
5565
## Setting up the Database - ORM
5666
This API is prepared to work with an SQL database, using [TypeORM](https://github.com/typeorm/typeorm). In this case we are using postgreSQL, and that is why in the package.json 'pg' has been included. If you where to use a different SQL database remember to install the correspondent driver.
5767

58-
The ORM configuration and connection to the database is in the file 'ormconfig.json'.
68+
The ORM configuration and connection to the database can be specified in the file 'ormconfig.json'. Here is directly in the connection to the database in 'server.ts' file because a environment variable containing databaseUrl is being used to set the connection data. This is prepared for Heroku, which provides a postgres-string-connection as env variable. In local is being mocked with the docker local postgres as can be seen in ".example.env"
69+
70+
It is importante to notice that, when serving the project directly with *.ts files using ts-node,the configuration for the ORM should specify the *.ts files path, but once the project is built (transpiled) and run as plain js, it will be needed to change it accordingly to find the built js files:
5971

60-
It is importante to notice that, when serving the project directly with *.ts files using ts-node, the ormconfig.json will work correctly, but once the project is built (transpiled) and run as plain js, it will be needed to change it in the ormconfig.js accordingly:
6172
```
6273
"entities": [
6374
"dist/entity/**/*.js"
@@ -70,7 +81,18 @@ It is importante to notice that, when serving the project directly with *.ts fil
7081
]
7182
```
7283

73-
You can find an implemented CRUD of the entity user in routes.ts file.
84+
Notice that if NODE_ENV is set to development, the ORM config won't be using SSL to connect to the DB. Otherwise it will.
85+
86+
```
87+
createConnection({
88+
...
89+
extra: {
90+
ssl: config.DbSslConn, // if not development, will use SSL
91+
}
92+
})
93+
```
94+
95+
You can find an implemented **CRUD of the entity user** in the correspondent controller controller/user.ts and its routes in routes.ts file.
7496

7597
## Entities validation
7698
This project uses the library class-validator, a decorator-based entity validation, which is used directly in the entities files as follows:
@@ -102,9 +124,10 @@ For further documentation regarding validations see [class-validator docs](https
102124
## Environment variables
103125
Create a .env file (or just rename the .example.env) containing all the env variables you want to set, dotenv library will take care of setting them. This project is using three variables at the moment:
104126

105-
* NODE_PORT -> port where the server will be started on
106-
* NODE_ENV -> environment, development value will set the logger as debug level, also important for CI
127+
* PORT -> port where the server will be started on, Heroku will set this env variable automatically
128+
* NODE_ENV -> environment, development value will set the logger as debug level, also important for CI. In addition will determine if the ORM connects to the DB through SSL or not.
107129
* JWT_SECRET -> secret value, JWT tokens should be signed with this value
130+
* DATABASE_URL -> DB connection data in connection-string format.
108131

109132
## Getting TypeScript
110133
TypeScript itself is simple to add to any project with `npm`.
@@ -135,7 +158,7 @@ The full folder structure of this app is explained below:
135158
| docker-compose.yml | Docker PostgreSQL and Adminer images in case you want to load the db from Docker |
136159
| tsconfig.json | Config settings for compiling server code written in TypeScript |
137160
| tslint.json | Config settings for TSLint code style checking |
138-
| ormconfig.json | Config settings for TypeORM and DB connection |
161+
| .example.env | Env variables file example to be renamed to .env |
139162

140163
## Configuring TypeScript compilation
141164
TypeScript uses the file `tsconfig.json` to adjust project compile options.
@@ -243,7 +266,7 @@ As can be found in the server.ts file, a JWT middleware has been added, passing
243266

244267
```
245268
// JWT middleware -> below this line, routes are only reached if JWT token is valid, secret as env variable
246-
app.use(jwt({ secret: process.env.JWT_SECRET }));
269+
app.use(jwt({ secret: config.jwtSecret }));
247270
```
248271
Go to the website [https://jwt.io/](https://jwt.io/) to create JWT tokens for testing/debugging purposes. Select algorithm HS256 and include the generated token in the Authorization header to pass through the jwt middleware.
249272

@@ -290,6 +313,7 @@ In that file you'll find two sections:
290313
| typeorm | A very cool SQL ORM. |
291314
| winston | Logging library. |
292315
| class-validator | Decorator based entities validation. |
316+
| pg-connection-string | Parser for database connection string |
293317

294318
## `devDependencies`
295319

0 commit comments

Comments
 (0)