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
+67-17Lines changed: 67 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,14 @@
2
2
3
3
## Description
4
4
5
-
This is a simple fasitfy application skeleton for JSON API server with some pre-defined features.
5
+
This is a simple file-based routing fasitfy application skeleton for JSON API server with some pre-defined features.
6
6
7
7
After calling the default exported function, the fastify server will listen on the specified host and port. The returned value of the function is a fastify instance. By default, this instance will also be registered to the global scope with variable name `app`. You can access it via `global.app` or just `app`. This name can be changed by setting the `app.globalAppVariable` in the config. To prevent this behavior, set the `app.disableGlobalAppVariable` to `true`.
8
8
9
+
## File-based routing
10
+
11
+
Any `{.js, .mjs, .ts}` file in the `app` and it's subfolders of your project root that does not starts with underscore(`_`) will be registered as a fastify plugin. The subfolder name will be the prefix. For example, if you have an `app/user/api.js` or `app/user/other-file.mjs` file, you can access it via `http://localhost:port/user/YOUR-API`.
12
+
9
13
## Reply helpers
10
14
11
15
There are some useful helpers can be used in the route handler. In the following example, the `req` is the first parameter of the handler, also know as `request`. The `res` is the second parameter of the handler function, also known as the `reply` object in fastify.
@@ -66,10 +70,6 @@ Generate an uncaught error response.
66
70
67
71
## Other features
68
72
69
-
### Database
70
-
71
-
If the `database` section is set in the config, it will also connect to the database using the `knex` package. The initialized knex instance will be stored in the `global.knex` variable in the process scope.
72
-
73
73
### Config object
74
74
75
75
The config object passed to the default exported function can be accessed via `app.config`
@@ -129,23 +129,20 @@ This handler can be disabled by setting the `app.disableApiErrorHandler` to `tru
129
129
130
130
By default, the server will log the API error(`throw new ApiError()`). This behavior can be disabled by setting the `app.disableLogApiError` to `true`.
131
131
132
-
### Orangize API prefix using folder
133
-
134
-
You can organize your API by putting them in different folders. The folder name will be the prefix of the API. For example, if you have an `app/user/api.js` file, you can access it via `http://localhost:port/user/YOUR-API`. Only the file with the `api.js` will be registered as fastify plugin.
135
-
136
132
## Usage
137
133
138
134
### Install
139
135
140
136
```bash
141
137
npm install fastify-app js-yaml
138
+
npm install knex mysql2 # if you want to use a database
142
139
```
143
140
144
141
> **Note**
145
142
>
146
143
> The `js-yaml` can be omitted if you don't want to use a yaml config file.
147
144
148
-
### Create a config file (optional)
145
+
### Create a config file
149
146
150
147
Create a `config.yaml` file in your project root with the following example:
151
148
@@ -200,11 +197,11 @@ database:
200
197
idleTimeoutMillis: 60000
201
198
```
202
199
203
-
> If you don't want this package to connect to a database, just remove the `database` section from the config.
200
+
### Create your first API endpoint
204
201
205
-
## Create your first API endpoint
202
+
> CommonJS and ES module are both supported.
206
203
207
-
Create an `app` folder in your project root, and create an `api.js` file in it with the following content:
204
+
Create an `app` folder in your project root, and create a js file in it, `api.js` for example (or `.mjs` or `.ts`), with the following content:
208
205
209
206
```javascript
210
207
'use strict';
@@ -230,9 +227,62 @@ function plugin(fastify, opts, done) {
230
227
231
228
After starting the server, this API endpoint can be accssed via `http://host:port/ok`.
232
229
233
-
You can also create subfolders in the `app` folder to organize your API. If you have an `app/user/api.js` file, you can access it via `http://localhost:port/user/YOUR-API`.
230
+
You can also create subfolders in the `app` folder to organize your API. If you have an `app/user/api.js` file, you can access it via `http://localhost:port/user/endpoint-in-api.js`.
231
+
232
+
Files with names starting with an underscore will not be registered to the fastify instance.
- The database is no longer initialized. You need to initialize the database connection yourself.
288
+
- Route files are no longer limited to `api.js`. Any file that does not start with an underscore and ends with `{.js, .mjs, .ts}` will be registered to the fastify instance.
0 commit comments