You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-9Lines changed: 33 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,11 @@ The main purpose of this repository is to build a good project setup and workflo
9
9
10
10
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.
11
11
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:
@@ -55,9 +65,10 @@ If you use Docker natively, the host for the server which you will need to inclu
55
65
## Setting up the Database - ORM
56
66
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.
57
67
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:
59
71
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:
61
72
```
62
73
"entities": [
63
74
"dist/entity/**/*.js"
@@ -70,7 +81,18 @@ It is importante to notice that, when serving the project directly with *.ts fil
70
81
]
71
82
```
72
83
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.
74
96
75
97
## Entities validation
76
98
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
102
124
## Environment variables
103
125
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:
104
126
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.
107
129
* JWT_SECRET -> secret value, JWT tokens should be signed with this value
130
+
* DATABASE_URL -> DB connection data in connection-string format.
108
131
109
132
## Getting TypeScript
110
133
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:
135
158
| docker-compose.yml | Docker PostgreSQL and Adminer images in case you want to load the db from Docker |
136
159
| tsconfig.json | Config settings for compiling server code written in TypeScript |
|ormconfig.json| Config settings for TypeORM and DB connection |
161
+
|.example.env| Env variables file example to be renamed to .env|
139
162
140
163
## Configuring TypeScript compilation
141
164
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
243
266
244
267
```
245
268
// 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 }));
247
270
```
248
271
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.
249
272
@@ -290,6 +313,7 @@ In that file you'll find two sections:
290
313
| typeorm | A very cool SQL ORM. |
291
314
| winston | Logging library. |
292
315
| class-validator | Decorator based entities validation. |
316
+
| pg-connection-string | Parser for database connection string |
0 commit comments