Skip to content

Commit a2f5906

Browse files
committed
more docs
1 parent 2a0b91a commit a2f5906

File tree

6 files changed

+165
-5
lines changed

6 files changed

+165
-5
lines changed

docs/docs/getting-started/docker.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ exa.js is intended to be used with or without docker in both development and pro
88

99
## compose file
1010

11+
the only item in the docker compose file that may need to be changed is the port number. by default it uses the same port that is configured in `config/master.js` for the app.
12+
1113
```yaml title="docker-compose.yml"
1214
services:
1315
app:
@@ -41,3 +43,20 @@ services:
4143
profiles:
4244
- nostart
4345
```
46+
47+
## helper script
48+
49+
for your convenience, a docker helper script is located in the root directory to help interact with an exa.js project running in docker more easily. the helper script has the following functions:
50+
51+
- `./docker.sh dev` - starts app in foreground
52+
- `./docker.sh start` - starts app in background
53+
- `./docker.sh stop` - stops app
54+
- `./docker.sh shell` - opens shell to the container running the app
55+
- `./docker.sh console <name>` - runs a console command by name
56+
- `./docker.sh jmig <command>` - runs jmig in the app container
57+
* requires the app to be running
58+
- `./docker.sh npm <args>` - runs npm in the root directory of the app
59+
60+
:::tip[Tip]
61+
if your project was initialized to use `bun`, swap `npm` for `bun` anywhere it appears
62+
:::

docs/docs/getting-started/project-structure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ contains express.js compatible middleware. middleware is configured for use in a
2727
contains [jmig](https://github.com/realtux/jmig) compatible database migrations files. these are used to apply and rollback changes to a database. please see the jmig readme for usage specifics.
2828

2929
### models
30-
contains sequelize.js compatible database model files. to use a different database orm, set `database.use = false` in `config/master.js` which will disable automatic initialization of the `models` folder. see docs for proper format.
30+
contains sequelize.js compatible database model files. to use a different database orm, set `database.use = false` in `config/master.js` which will disable automatic initialization of the `models` directory. see docs for proper format.
3131

3232
### public
3333
contains publicly available static files. in development, this is served with `express.static()` at base url `/public`. in production, this should be served with a normal web server.

docs/docs/modules/console.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,30 @@ export default new class {
2323
}
2424

2525
};
26+
```
27+
28+
## running a console command
29+
30+
all the following examples assume a script called `sample` is located at `console/sample.js`.
31+
32+
### from root directory
33+
34+
```bash
35+
npm run sample
36+
```
37+
38+
### via a cronjob
39+
40+
```bash
41+
npm --prefix /path/to/app run sample
42+
```
43+
44+
### while using docker
45+
46+
```bash
47+
# from root directory
48+
./docker.sh console sample
2649

50+
# via cronjob
51+
/path/to/app/docker.sh console sample
2752
```

docs/docs/modules/http.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# http
6+
7+
this module is used for exposing http routes for use in apis and other purposes.
8+
9+
## concept
10+
11+
files are organized within the `http` directory in any manner desired. a common pattern used is to have directories in the same structure that your urls will ultimately be, however, this is entirely up to you. exa.js on start will process all files recursively that are present in the `http` directory.
12+
13+
## internal handling
14+
15+
http server and routes and implemented using express.js, a well known and trusted http server. everything express.js supports is also supported in exa.js.
16+
17+
## anatomy of an http file
18+
19+
the following is the structure of an http file
20+
21+
```js title="http/sample.js"
22+
import authorized from '#app/middleware/authorized.js';
23+
24+
export default new class {
25+
26+
routes = {
27+
'get /hello/world': 'world',
28+
}
29+
30+
middleware = {
31+
'*': [authorized]
32+
}
33+
34+
async world(req, res) {
35+
return res
36+
.status(200)
37+
.send({
38+
message: 'hello world'
39+
});
40+
}
41+
42+
};
43+
```
44+
45+
### routes
46+
47+
each route is a key composed of the http method and route path or pattern with a string value of the name of the method it should call. because this just wraps express.js internally, it supports everything express.js supports, including:
48+
49+
basic routes
50+
```js
51+
routes = {
52+
'get /hello/world': 'world',
53+
}
54+
```
55+
56+
regex routes
57+
```js
58+
routes = {
59+
'get /pets/(dog|cat)': 'pet',
60+
}
61+
```
62+
63+
param routes
64+
```js
65+
routes = {
66+
'get /blogs/:blog_id': 'get_blog',
67+
}
68+
```
69+
70+
### middleware
71+
72+
one or more pieces of middleware can be ran prior to the route method being called. this middleware is great for many purposes such as authentication, setting context variables, or performing pre-route logic.
73+
74+
a middleware file looks like this:
75+
76+
```js title="middleware/authorized.js"
77+
export default async (req, res, next) => {
78+
// middleware logic
79+
if (req.headers.authorization !== 'abc123') {
80+
return res
81+
.status(401)
82+
.send();
83+
}
84+
85+
return next();
86+
};
87+
```
88+
89+
this middleware checks an api key to see if it's the right value. if it's not, a 401 unauthorized response is received, otherwise `next()` is called which either calls the next middleware in the chain or calls the route method.
90+
91+
to use middleware, add key/value pairs to the `middleware` object. the key should be the method name and the value should be an array of middleware to run.
92+
93+
```js
94+
// apply authorized middleware to all routes
95+
middleware = {
96+
'*': [authorized],
97+
}
98+
99+
// apply authorized middleware to one route
100+
middleware = {
101+
hello: [authorized],
102+
}
103+
104+
// apply authorized middleware to all routes except for hello
105+
middleware = {
106+
'*': [authorized],
107+
hello: []
108+
}
109+
110+
// apply common and authorized middleware to all routes
111+
middleware = {
112+
'*': [common, authorized],
113+
}
114+
```
115+
116+
middleware precedence is top to bottom. this is why everything works in the third example. `hello` has a value of blank array.

docs/docs/modules/websockets.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 4
33
---
44

55
# websockets
@@ -8,13 +8,13 @@ this module is used for exposing urls that upgrade to websocket connections.
88

99
## concept
1010

11-
zero or more files are created in the `websocket` directory. each file is intended to be used for a single url that upgrades to websocket. the file can be named anything and has no effect on functionality. all files in the `websocket` folder will be loaded and processed when `exa.js` starts.
11+
zero or more files are created in the `websocket` directory. each file is intended to be used for a single url that upgrades to websocket. the file can be named anything and has no effect on functionality. all files in the `websocket` directory will be loaded and processed when `exa.js` starts.
1212

1313
## internal handling
1414

1515
the websocket server implementation is handled using the well known and trusted `ws` module. this module largely just wraps `wss.on('connection', callback)`. connections are upgraded from regular http requests rather than running entirely separate servers on separate ports. this makes enabling websockets very easy.
1616

17-
## file structure
17+
## anatomy of a websocket handler
1818

1919
a websocket handler looks like this:
2020

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ initializing will create the exa.js template project structure in the current di
5151
*contains [jmig](https://github.com/realtux/jmig) compatible database migrations files. these are used to apply and rollback changes to a database. please see the jmig readme for usage specifics.*
5252

5353
- **models**
54-
*contains sequelize.js compatible database model files. to use a different database orm, set `database.use = false` in `config/master.js` which will disable automatic initialization of the `models` folder. see docs for proper format.*
54+
*contains sequelize.js compatible database model files. to use a different database orm, set `database.use = false` in `config/master.js` which will disable automatic initialization of the `models` directory. see docs for proper format.*
5555

5656
- **public**
5757
*contains publicly available static files. in development, this is served with `express.static()` at base url `/public`. in production, this should be served with a normal web server.*

0 commit comments

Comments
 (0)