lien 4.0.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOCUMENTATION.md +4 -290
- package/LICENSE +1 -1
- package/README.md +14 -321
- package/assets/logo.png +0 -0
- package/example/index.js +8 -10
- package/example/login.js +23 -36
- package/example/secure.js +6 -10
- package/lib/errors.js +58 -0
- package/lib/hook-registry.js +221 -0
- package/lib/hooks.js +302 -0
- package/lib/index.js +15 -1117
- package/lib/lien.js +539 -0
- package/lib/server.js +590 -0
- package/lib/sessions.js +56 -0
- package/lib/to-buffer.js +32 -45
- package/lib/utils.js +75 -0
- package/package.json +34 -21
- package/.github/FUNDING.yml +0 -4
- package/.travis.yml +0 -3
- package/CONTRIBUTING.md +0 -66
- package/test/index.js +0 -280
- package/test/public/foo/index.html +0 -1
- package/test/public/index.html +0 -12
- package/test/public/test.html +0 -1
- package/test/views/main.ajs +0 -1
package/DOCUMENTATION.md
CHANGED
|
@@ -2,297 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
You can see below the API reference of this module.
|
|
4
4
|
|
|
5
|
-
###
|
|
6
|
-
Creates the `lien` object.
|
|
5
|
+
### Hooks
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
The constructor for reusable hook pipelines.
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
- **Object** `res`: The response object.
|
|
12
|
-
- **Function** `next`: The `next` middleware function.
|
|
13
|
-
- **Object** `server`: The `Lien` server instance.
|
|
9
|
+
### LienObj
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
- **Lien** The lien object.
|
|
17
|
-
|
|
18
|
-
### `next()`
|
|
19
|
-
Go to the next middleware handler.
|
|
20
|
-
|
|
21
|
-
#### Return
|
|
22
|
-
- **Lien** The `Lien` instance.
|
|
23
|
-
|
|
24
|
-
### `redirect(newUrl, query)`
|
|
25
|
-
Redirects the client to another url.
|
|
26
|
-
|
|
27
|
-
#### Params
|
|
28
|
-
|
|
29
|
-
- **String** `newUrl`: The new url to redirect to.
|
|
30
|
-
- **Boolean|Object** `query`: If `true`, the request querystring parameters will be appended. If it's an object, it will be merged with the request querystring parameters.
|
|
31
|
-
|
|
32
|
-
### `render(template, data)`
|
|
33
|
-
Renders a template to the client.
|
|
34
|
-
|
|
35
|
-
#### Params
|
|
36
|
-
|
|
37
|
-
- **String** `template`: The template name.
|
|
38
|
-
- **Object** `data`: The template data.
|
|
39
|
-
|
|
40
|
-
### `startSession(data)`
|
|
41
|
-
Starts a session.
|
|
42
|
-
|
|
43
|
-
#### Params
|
|
44
|
-
|
|
45
|
-
- **Object** `data`: The session data.
|
|
46
|
-
|
|
47
|
-
### `setSessionData(data)`
|
|
48
|
-
Sets the session data.
|
|
49
|
-
|
|
50
|
-
#### Params
|
|
51
|
-
|
|
52
|
-
- **Object** `data`: The session data.
|
|
53
|
-
|
|
54
|
-
### `getSessionData(field)`
|
|
55
|
-
Returns the session data object/specific field.
|
|
56
|
-
|
|
57
|
-
#### Params
|
|
58
|
-
|
|
59
|
-
- **Field** `field`: A specific field to get from the session object.
|
|
60
|
-
|
|
61
|
-
#### Return
|
|
62
|
-
- **Value|Object** The field value. If a field is not specified, the whole session data object is returned.
|
|
63
|
-
|
|
64
|
-
### `destroySession()`
|
|
65
|
-
Destroys the session.
|
|
66
|
-
|
|
67
|
-
### `header(name, value)`
|
|
68
|
-
Gets/sets/deletes headers.
|
|
69
|
-
|
|
70
|
-
#### Params
|
|
71
|
-
|
|
72
|
-
- **String** `name`: The header name.
|
|
73
|
-
- **String** `value`: The header value to set. If `null`, the header will be *removed*.
|
|
74
|
-
|
|
75
|
-
#### Return
|
|
76
|
-
- **Lien** The Lien instance.
|
|
77
|
-
|
|
78
|
-
### `apiMsg(msg, status)`
|
|
79
|
-
Sends to the client a JSON object containing the `message` field.
|
|
80
|
-
|
|
81
|
-
#### Params
|
|
82
|
-
|
|
83
|
-
- **String** `msg`: The API message.
|
|
84
|
-
- **Number** `status`: The status code (default: `200`).
|
|
85
|
-
|
|
86
|
-
### `apiError(msg, status)`
|
|
87
|
-
Like `apiMsg`, but by default with a status code of `422`.
|
|
88
|
-
|
|
89
|
-
#### Params
|
|
90
|
-
|
|
91
|
-
- **String** `msg`: The API message.
|
|
92
|
-
- **Number** `status`: The status code (default: `422`).
|
|
93
|
-
|
|
94
|
-
### `end(content, status, contentType, headers)`
|
|
95
|
-
Ends the response sending the content.
|
|
96
|
-
|
|
97
|
-
#### Params
|
|
98
|
-
|
|
99
|
-
- **Anything** `content`: The content that should be sent to the response.
|
|
100
|
-
- **Number** `status`: The status code.
|
|
101
|
-
- **String** `contentType`: The content type (e.g. `"json"`).
|
|
102
|
-
- **Object** `headers`: Additional headers to send in the response.
|
|
103
|
-
|
|
104
|
-
### `cookie(cookie, value)`
|
|
105
|
-
Sets, gets or deletes the cookie.
|
|
106
|
-
|
|
107
|
-
#### Params
|
|
108
|
-
|
|
109
|
-
- **String** `cookie`: The searched cookie.
|
|
110
|
-
- **String** `value`: If provided and it not `null`, the cookie will be set. If it's null, the cookie will be deleted. If `value` is not provided, the cookie value will be returned.
|
|
111
|
-
|
|
112
|
-
#### Return
|
|
113
|
-
- **String|null|undefined** `null`, if the cookie was deleted. `undefined` if the cookie doesn't exist. The cookie value if this exists.
|
|
114
|
-
|
|
115
|
-
### `file(path, customRoot)`
|
|
116
|
-
Serves a file to the response.
|
|
117
|
-
|
|
118
|
-
#### Params
|
|
119
|
-
|
|
120
|
-
- **String** `path`: Relative path to the file.
|
|
121
|
-
- **String** `customRoot`: Absolute path to the root directory (optional).
|
|
122
|
-
|
|
123
|
-
### `Lien(opt_options)`
|
|
124
|
-
Creates a new Lien instance.
|
|
125
|
-
|
|
126
|
-
It extends the `EventEmitter` class.
|
|
127
|
-
|
|
128
|
-
It emits the following events:
|
|
129
|
-
|
|
130
|
-
- `load` (err): After the server is started. If there are no errors, the `err` will be null.
|
|
131
|
-
- `serverError` (err, req, res): This is emitted when something goes wrong after the server is started.
|
|
132
|
-
- `error` (err): Errors which may appear during the server initialization.
|
|
133
|
-
|
|
134
|
-
#### Params
|
|
135
|
-
|
|
136
|
-
- **Object** `opt_options`: An object containing the following properties:
|
|
137
|
-
- `host` (String): The server host.
|
|
138
|
-
- `port` (Integer): The server port. `process.env.PORT` is used, default is `3000`.
|
|
139
|
-
- `session` (Boolean|Object): Enable the session support. If it's an object, it will be merged with the following defaults and passed to [`express-session`](https://github.com/expressjs/session):
|
|
140
|
-
- `secret` (String): This is the secret used to sign the session ID cookie (default: "lien server").
|
|
141
|
-
- `resave` (Boolean): Forces the session to be saved back to the session store, even if the session was never modified during the request (default: false).
|
|
142
|
-
- `saveUninitialized` (Boolean): Forces a session that is "uninitialized" to be saved to the store (default: `true`).
|
|
143
|
-
- `cookie` (Object): The cookie [options](https://github.com/expressjs/cookie-parser).
|
|
144
|
-
- `storeOptions` (Object): The session store options. These options are passed to the session store you choose.
|
|
145
|
-
- `store`: (String|Function): The session store name or function. By default it's using a memory store if the session is enabled.
|
|
146
|
-
- `public` (String|Array): The path to the public directory or an array of arrays in this format: `["/url/of/static/dir", "path/to/static/dir"]`.
|
|
147
|
-
|
|
148
|
-
Example:
|
|
149
|
-
|
|
150
|
-
```js
|
|
151
|
-
[
|
|
152
|
-
["/images", "path/to/images"]
|
|
153
|
-
, ["/", "path/to/public"]
|
|
154
|
-
]
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
- `ssl` (Object): An object containing the following fields:
|
|
158
|
-
- `key` (String): The path to the key file.
|
|
159
|
-
- `cert` (String): The path to the cert file.
|
|
160
|
-
- `_key` (String|Buffer): The key file content (defaults to the key file content).
|
|
161
|
-
- `_cert` (String|Buffer): The cert file content (defaults to the cert file content).
|
|
162
|
-
|
|
163
|
-
- `views`
|
|
164
|
-
- `path` (String): The path to the views directory.
|
|
165
|
-
- `name` (String): The view engine name.
|
|
166
|
-
|
|
167
|
-
- `errorPages` (Object):
|
|
168
|
-
- `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
169
|
-
- `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
170
|
-
- `badCsrf` (String|Function): The path to a custom bad CSRF page or a function receiving the lien object as parameter. This can be used to serve custom bad CSRF errors.
|
|
171
|
-
|
|
172
|
-
- `logErrors` (Boolean): Log the server errors (default: `true`).
|
|
173
|
-
- `csrf` (Object): The CSRF options. These are passed to [`csurf`](https://github.com/expressjs/csurf)
|
|
174
|
-
- `bodyParserJson` (Boolean): Whether to enable the JSON body parser (default: `true`)
|
|
175
|
-
- `bodyParserUrlEncoded` (Boolean): Whether to enable the URL encoded body parser (default: `true`)
|
|
176
|
-
- `cookieParser` (Boolean): Whether to enable the cookie parser (default: `true`)
|
|
177
|
-
- `transformers` (Boolean): Whether to enable the transformers (default: `true`)
|
|
178
|
-
|
|
179
|
-
#### Return
|
|
180
|
-
- **Object** The Lien instance.
|
|
181
|
-
|
|
182
|
-
### `addStaticPath(url, localPath)`
|
|
183
|
-
Adds a new static path to the server.
|
|
184
|
-
|
|
185
|
-
#### Params
|
|
186
|
-
|
|
187
|
-
- **String** `url`: The static path url endpoint.
|
|
188
|
-
- **String** `localPath`: The local path to the directory.
|
|
189
|
-
|
|
190
|
-
### `addPage(url, method, output)`
|
|
191
|
-
Adds a new page to be handled.
|
|
192
|
-
|
|
193
|
-
#### Params
|
|
194
|
-
|
|
195
|
-
- **String** `url`: The page url.
|
|
196
|
-
- **String|Object** `method`: The request methods to be handled (default: `"all"`) or an object:
|
|
197
|
-
- `method` (String): The HTTP method.
|
|
198
|
-
- `before` (Array|Function): A function or an array of middleware functions to be executed *before* the main function.
|
|
199
|
-
- `after` (Array|Function): A function or an array of middleware functions to be executed *after* the main function.
|
|
200
|
-
- **Function** `output`: A function receiving the `lien` object as parameter. If can be a path serving a public file.
|
|
201
|
-
|
|
202
|
-
### `errorPages(options)`
|
|
203
|
-
Handle the error pages.
|
|
204
|
-
|
|
205
|
-
#### Params
|
|
206
|
-
|
|
207
|
-
- **Object** `options`: An object containing the following fields:
|
|
208
|
-
- `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
209
|
-
- `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
210
|
-
|
|
211
|
-
### `getHooks(type, url, method)`
|
|
212
|
-
Gets the transformer for a url.
|
|
213
|
-
|
|
214
|
-
#### Params
|
|
215
|
-
|
|
216
|
-
- **String** `type`: The hook type (`before` or `after`).
|
|
217
|
-
- **String** `url`: The url.
|
|
218
|
-
- **String** `method`: The method.
|
|
219
|
-
|
|
220
|
-
#### Return
|
|
221
|
-
- **Transformer|null** The transformer (if it exists) or `null`.
|
|
222
|
-
|
|
223
|
-
### `getHooksStrict(type, url, method)`
|
|
224
|
-
Similar to `getHooks`, but doesn't concat hooks based on the regex
|
|
225
|
-
matching but only if they are the same regex.
|
|
226
|
-
|
|
227
|
-
#### Params
|
|
228
|
-
|
|
229
|
-
- **String** `type`: The hook type (`before` or `after`).
|
|
230
|
-
- **String** `url`: The url.
|
|
231
|
-
- **String** `method`: The method.
|
|
232
|
-
|
|
233
|
-
#### Return
|
|
234
|
-
- **Transformer|null** The transformer (if it exists) or `null`.
|
|
235
|
-
|
|
236
|
-
### `insertHook(type, url, method, trans)`
|
|
237
|
-
Inserts a new hook.
|
|
238
|
-
|
|
239
|
-
#### Params
|
|
240
|
-
|
|
241
|
-
- **String** `type`: The hook type (`before`, `after`, `custom:name`).
|
|
242
|
-
- **String** `url`: The url.
|
|
243
|
-
- **String** `method`: The method.
|
|
244
|
-
- **Transformer** `trans`: The transformer to insert.
|
|
245
|
-
|
|
246
|
-
#### Return
|
|
247
|
-
- **Transformer** The inserted transformer.
|
|
248
|
-
|
|
249
|
-
### `hook(where, url, method, cb, transType)`
|
|
250
|
-
Adds a new hook.
|
|
251
|
-
|
|
252
|
-
#### Params
|
|
253
|
-
|
|
254
|
-
- **String** `where`: The hook type (`before` or `after`).
|
|
255
|
-
- **String** `url`: The route url.
|
|
256
|
-
- **String** `method`: The HTTP method.
|
|
257
|
-
- **Function** `cb`: The callback function.
|
|
258
|
-
- **Number** `transType`: The transformer type.
|
|
259
|
-
|
|
260
|
-
### `before(url, method, cb, transType)`
|
|
261
|
-
Adds a before hook. It will handle all the subroutes of the `url`.
|
|
262
|
-
|
|
263
|
-
#### Params
|
|
264
|
-
|
|
265
|
-
- **String** `url`: The route url.
|
|
266
|
-
- **String** `method`: The HTTP method.
|
|
267
|
-
- **Function** `cb`: The callback function.
|
|
268
|
-
- **Number** `transType`: The transformer type.
|
|
269
|
-
|
|
270
|
-
### `after(url, method, cb, transType)`
|
|
271
|
-
Adds a before hook. It will handle all the subroutes of the `url`.
|
|
272
|
-
|
|
273
|
-
#### Params
|
|
274
|
-
|
|
275
|
-
- **String** `url`: The route url.
|
|
276
|
-
- **String** `method`: The HTTP method.
|
|
277
|
-
- **Function** `cb`: The callback function.
|
|
278
|
-
- **Number** `transType`: The transformer type.
|
|
279
|
-
|
|
280
|
-
### `use(url, method, cb, transType)`
|
|
281
|
-
Use this function to add middleware handlers.
|
|
282
|
-
|
|
283
|
-
#### Params
|
|
284
|
-
|
|
285
|
-
- **String** `url`: The route url.
|
|
286
|
-
- **String** `method`: The HTTP method.
|
|
287
|
-
- **Function** `cb`: The callback function.
|
|
288
|
-
- **Number** `transType`: The transformer type.
|
|
289
|
-
|
|
290
|
-
### `add(url, method, cb)`
|
|
291
|
-
Adds a new middleware. **Note**: This will *not* trigger the hooks.
|
|
292
|
-
|
|
293
|
-
#### Params
|
|
294
|
-
|
|
295
|
-
- **String** `url`: The endpoint url.
|
|
296
|
-
- **String** `method`: The HTTP method (default: `all`).
|
|
297
|
-
- **Function** `cb`: The callback function.
|
|
11
|
+
The request object constructor, also available as a named export.
|
|
298
12
|
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2014-
|
|
3
|
+
Copyright (c) 2014-26 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net/)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
[](#)
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
|
|
22
22
|
# lien
|
|
23
23
|
|
|
24
|
-
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [![
|
|
24
|
+
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [](https://www.npmjs.com/package/lien) [](https://www.npmjs.com/package/lien) [](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github)
|
|
25
25
|
|
|
26
26
|
<a href="https://www.buymeacoffee.com/H96WwChMy" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee"></a>
|
|
27
27
|
|
|
@@ -76,12 +76,12 @@ yarn add lien
|
|
|
76
76
|
|
|
77
77
|
|
|
78
78
|
```js
|
|
79
|
-
|
|
79
|
+
import Lien from "lien";
|
|
80
80
|
|
|
81
81
|
// Init lien server
|
|
82
82
|
const server = new Lien({
|
|
83
|
-
port: 9000
|
|
84
|
-
|
|
83
|
+
port: 9000,
|
|
84
|
+
public: `${import.meta.dirname}/public`
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
// Listen for load
|
|
@@ -119,316 +119,34 @@ server.on("serverError", err => {
|
|
|
119
119
|
|
|
120
120
|
|
|
121
121
|
|
|
122
|
-
## :question: Get Help
|
|
123
|
-
|
|
124
|
-
There are few ways to get help:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
129
|
-
2. For bug reports and feature requests, open issues. :bug:
|
|
130
|
-
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
122
|
## :memo: Documentation
|
|
137
123
|
|
|
138
124
|
|
|
139
|
-
###
|
|
140
|
-
Creates the `lien` object.
|
|
141
|
-
|
|
142
|
-
#### Params
|
|
143
|
-
|
|
144
|
-
- **Object** `req`: The request object.
|
|
145
|
-
- **Object** `res`: The response object.
|
|
146
|
-
- **Function** `next`: The `next` middleware function.
|
|
147
|
-
- **Object** `server`: The `Lien` server instance.
|
|
148
|
-
|
|
149
|
-
#### Return
|
|
150
|
-
- **Lien** The lien object.
|
|
151
|
-
|
|
152
|
-
### `next()`
|
|
153
|
-
Go to the next middleware handler.
|
|
154
|
-
|
|
155
|
-
#### Return
|
|
156
|
-
- **Lien** The `Lien` instance.
|
|
157
|
-
|
|
158
|
-
### `redirect(newUrl, query)`
|
|
159
|
-
Redirects the client to another url.
|
|
160
|
-
|
|
161
|
-
#### Params
|
|
162
|
-
|
|
163
|
-
- **String** `newUrl`: The new url to redirect to.
|
|
164
|
-
- **Boolean|Object** `query`: If `true`, the request querystring parameters will be appended. If it's an object, it will be merged with the request querystring parameters.
|
|
165
|
-
|
|
166
|
-
### `render(template, data)`
|
|
167
|
-
Renders a template to the client.
|
|
168
|
-
|
|
169
|
-
#### Params
|
|
170
|
-
|
|
171
|
-
- **String** `template`: The template name.
|
|
172
|
-
- **Object** `data`: The template data.
|
|
173
|
-
|
|
174
|
-
### `startSession(data)`
|
|
175
|
-
Starts a session.
|
|
176
|
-
|
|
177
|
-
#### Params
|
|
178
|
-
|
|
179
|
-
- **Object** `data`: The session data.
|
|
180
|
-
|
|
181
|
-
### `setSessionData(data)`
|
|
182
|
-
Sets the session data.
|
|
183
|
-
|
|
184
|
-
#### Params
|
|
185
|
-
|
|
186
|
-
- **Object** `data`: The session data.
|
|
187
|
-
|
|
188
|
-
### `getSessionData(field)`
|
|
189
|
-
Returns the session data object/specific field.
|
|
190
|
-
|
|
191
|
-
#### Params
|
|
192
|
-
|
|
193
|
-
- **Field** `field`: A specific field to get from the session object.
|
|
194
|
-
|
|
195
|
-
#### Return
|
|
196
|
-
- **Value|Object** The field value. If a field is not specified, the whole session data object is returned.
|
|
197
|
-
|
|
198
|
-
### `destroySession()`
|
|
199
|
-
Destroys the session.
|
|
200
|
-
|
|
201
|
-
### `header(name, value)`
|
|
202
|
-
Gets/sets/deletes headers.
|
|
203
|
-
|
|
204
|
-
#### Params
|
|
205
|
-
|
|
206
|
-
- **String** `name`: The header name.
|
|
207
|
-
- **String** `value`: The header value to set. If `null`, the header will be *removed*.
|
|
208
|
-
|
|
209
|
-
#### Return
|
|
210
|
-
- **Lien** The Lien instance.
|
|
211
|
-
|
|
212
|
-
### `apiMsg(msg, status)`
|
|
213
|
-
Sends to the client a JSON object containing the `message` field.
|
|
214
|
-
|
|
215
|
-
#### Params
|
|
216
|
-
|
|
217
|
-
- **String** `msg`: The API message.
|
|
218
|
-
- **Number** `status`: The status code (default: `200`).
|
|
219
|
-
|
|
220
|
-
### `apiError(msg, status)`
|
|
221
|
-
Like `apiMsg`, but by default with a status code of `422`.
|
|
222
|
-
|
|
223
|
-
#### Params
|
|
224
|
-
|
|
225
|
-
- **String** `msg`: The API message.
|
|
226
|
-
- **Number** `status`: The status code (default: `422`).
|
|
227
|
-
|
|
228
|
-
### `end(content, status, contentType, headers)`
|
|
229
|
-
Ends the response sending the content.
|
|
230
|
-
|
|
231
|
-
#### Params
|
|
232
|
-
|
|
233
|
-
- **Anything** `content`: The content that should be sent to the response.
|
|
234
|
-
- **Number** `status`: The status code.
|
|
235
|
-
- **String** `contentType`: The content type (e.g. `"json"`).
|
|
236
|
-
- **Object** `headers`: Additional headers to send in the response.
|
|
125
|
+
### Hooks
|
|
237
126
|
|
|
238
|
-
|
|
239
|
-
Sets, gets or deletes the cookie.
|
|
127
|
+
The constructor for reusable hook pipelines.
|
|
240
128
|
|
|
241
|
-
|
|
129
|
+
### LienObj
|
|
242
130
|
|
|
243
|
-
|
|
244
|
-
- **String** `value`: If provided and it not `null`, the cookie will be set. If it's null, the cookie will be deleted. If `value` is not provided, the cookie value will be returned.
|
|
131
|
+
The request object constructor, also available as a named export.
|
|
245
132
|
|
|
246
|
-
#### Return
|
|
247
|
-
- **String|null|undefined** `null`, if the cookie was deleted. `undefined` if the cookie doesn't exist. The cookie value if this exists.
|
|
248
133
|
|
|
249
|
-
### `file(path, customRoot)`
|
|
250
|
-
Serves a file to the response.
|
|
251
134
|
|
|
252
|
-
#### Params
|
|
253
135
|
|
|
254
|
-
- **String** `path`: Relative path to the file.
|
|
255
|
-
- **String** `customRoot`: Absolute path to the root directory (optional).
|
|
256
136
|
|
|
257
|
-
### `Lien(opt_options)`
|
|
258
|
-
Creates a new Lien instance.
|
|
259
137
|
|
|
260
|
-
It extends the `EventEmitter` class.
|
|
261
138
|
|
|
262
|
-
It emits the following events:
|
|
263
139
|
|
|
264
|
-
- `load` (err): After the server is started. If there are no errors, the `err` will be null.
|
|
265
|
-
- `serverError` (err, req, res): This is emitted when something goes wrong after the server is started.
|
|
266
|
-
- `error` (err): Errors which may appear during the server initialization.
|
|
267
140
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
- **Object** `opt_options`: An object containing the following properties:
|
|
271
|
-
- `host` (String): The server host.
|
|
272
|
-
- `port` (Integer): The server port. `process.env.PORT` is used, default is `3000`.
|
|
273
|
-
- `session` (Boolean|Object): Enable the session support. If it's an object, it will be merged with the following defaults and passed to [`express-session`](https://github.com/expressjs/session):
|
|
274
|
-
- `secret` (String): This is the secret used to sign the session ID cookie (default: "lien server").
|
|
275
|
-
- `resave` (Boolean): Forces the session to be saved back to the session store, even if the session was never modified during the request (default: false).
|
|
276
|
-
- `saveUninitialized` (Boolean): Forces a session that is "uninitialized" to be saved to the store (default: `true`).
|
|
277
|
-
- `cookie` (Object): The cookie [options](https://github.com/expressjs/cookie-parser).
|
|
278
|
-
- `storeOptions` (Object): The session store options. These options are passed to the session store you choose.
|
|
279
|
-
- `store`: (String|Function): The session store name or function. By default it's using a memory store if the session is enabled.
|
|
280
|
-
- `public` (String|Array): The path to the public directory or an array of arrays in this format: `["/url/of/static/dir", "path/to/static/dir"]`.
|
|
281
|
-
|
|
282
|
-
Example:
|
|
283
|
-
|
|
284
|
-
```js
|
|
285
|
-
[
|
|
286
|
-
["/images", "path/to/images"]
|
|
287
|
-
, ["/", "path/to/public"]
|
|
288
|
-
]
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
- `ssl` (Object): An object containing the following fields:
|
|
292
|
-
- `key` (String): The path to the key file.
|
|
293
|
-
- `cert` (String): The path to the cert file.
|
|
294
|
-
- `_key` (String|Buffer): The key file content (defaults to the key file content).
|
|
295
|
-
- `_cert` (String|Buffer): The cert file content (defaults to the cert file content).
|
|
296
|
-
|
|
297
|
-
- `views`
|
|
298
|
-
- `path` (String): The path to the views directory.
|
|
299
|
-
- `name` (String): The view engine name.
|
|
300
|
-
|
|
301
|
-
- `errorPages` (Object):
|
|
302
|
-
- `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
303
|
-
- `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
304
|
-
- `badCsrf` (String|Function): The path to a custom bad CSRF page or a function receiving the lien object as parameter. This can be used to serve custom bad CSRF errors.
|
|
305
|
-
|
|
306
|
-
- `logErrors` (Boolean): Log the server errors (default: `true`).
|
|
307
|
-
- `csrf` (Object): The CSRF options. These are passed to [`csurf`](https://github.com/expressjs/csurf)
|
|
308
|
-
- `bodyParserJson` (Boolean): Whether to enable the JSON body parser (default: `true`)
|
|
309
|
-
- `bodyParserUrlEncoded` (Boolean): Whether to enable the URL encoded body parser (default: `true`)
|
|
310
|
-
- `cookieParser` (Boolean): Whether to enable the cookie parser (default: `true`)
|
|
311
|
-
- `transformers` (Boolean): Whether to enable the transformers (default: `true`)
|
|
312
|
-
|
|
313
|
-
#### Return
|
|
314
|
-
- **Object** The Lien instance.
|
|
315
|
-
|
|
316
|
-
### `addStaticPath(url, localPath)`
|
|
317
|
-
Adds a new static path to the server.
|
|
318
|
-
|
|
319
|
-
#### Params
|
|
320
|
-
|
|
321
|
-
- **String** `url`: The static path url endpoint.
|
|
322
|
-
- **String** `localPath`: The local path to the directory.
|
|
323
|
-
|
|
324
|
-
### `addPage(url, method, output)`
|
|
325
|
-
Adds a new page to be handled.
|
|
326
|
-
|
|
327
|
-
#### Params
|
|
328
|
-
|
|
329
|
-
- **String** `url`: The page url.
|
|
330
|
-
- **String|Object** `method`: The request methods to be handled (default: `"all"`) or an object:
|
|
331
|
-
- `method` (String): The HTTP method.
|
|
332
|
-
- `before` (Array|Function): A function or an array of middleware functions to be executed *before* the main function.
|
|
333
|
-
- `after` (Array|Function): A function or an array of middleware functions to be executed *after* the main function.
|
|
334
|
-
- **Function** `output`: A function receiving the `lien` object as parameter. If can be a path serving a public file.
|
|
335
|
-
|
|
336
|
-
### `errorPages(options)`
|
|
337
|
-
Handle the error pages.
|
|
338
|
-
|
|
339
|
-
#### Params
|
|
340
|
-
|
|
341
|
-
- **Object** `options`: An object containing the following fields:
|
|
342
|
-
- `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
343
|
-
- `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
344
|
-
|
|
345
|
-
### `getHooks(type, url, method)`
|
|
346
|
-
Gets the transformer for a url.
|
|
347
|
-
|
|
348
|
-
#### Params
|
|
349
|
-
|
|
350
|
-
- **String** `type`: The hook type (`before` or `after`).
|
|
351
|
-
- **String** `url`: The url.
|
|
352
|
-
- **String** `method`: The method.
|
|
353
|
-
|
|
354
|
-
#### Return
|
|
355
|
-
- **Transformer|null** The transformer (if it exists) or `null`.
|
|
356
|
-
|
|
357
|
-
### `getHooksStrict(type, url, method)`
|
|
358
|
-
Similar to `getHooks`, but doesn't concat hooks based on the regex
|
|
359
|
-
matching but only if they are the same regex.
|
|
360
|
-
|
|
361
|
-
#### Params
|
|
362
|
-
|
|
363
|
-
- **String** `type`: The hook type (`before` or `after`).
|
|
364
|
-
- **String** `url`: The url.
|
|
365
|
-
- **String** `method`: The method.
|
|
366
|
-
|
|
367
|
-
#### Return
|
|
368
|
-
- **Transformer|null** The transformer (if it exists) or `null`.
|
|
369
|
-
|
|
370
|
-
### `insertHook(type, url, method, trans)`
|
|
371
|
-
Inserts a new hook.
|
|
372
|
-
|
|
373
|
-
#### Params
|
|
374
|
-
|
|
375
|
-
- **String** `type`: The hook type (`before`, `after`, `custom:name`).
|
|
376
|
-
- **String** `url`: The url.
|
|
377
|
-
- **String** `method`: The method.
|
|
378
|
-
- **Transformer** `trans`: The transformer to insert.
|
|
379
|
-
|
|
380
|
-
#### Return
|
|
381
|
-
- **Transformer** The inserted transformer.
|
|
382
|
-
|
|
383
|
-
### `hook(where, url, method, cb, transType)`
|
|
384
|
-
Adds a new hook.
|
|
385
|
-
|
|
386
|
-
#### Params
|
|
387
|
-
|
|
388
|
-
- **String** `where`: The hook type (`before` or `after`).
|
|
389
|
-
- **String** `url`: The route url.
|
|
390
|
-
- **String** `method`: The HTTP method.
|
|
391
|
-
- **Function** `cb`: The callback function.
|
|
392
|
-
- **Number** `transType`: The transformer type.
|
|
393
|
-
|
|
394
|
-
### `before(url, method, cb, transType)`
|
|
395
|
-
Adds a before hook. It will handle all the subroutes of the `url`.
|
|
396
|
-
|
|
397
|
-
#### Params
|
|
398
|
-
|
|
399
|
-
- **String** `url`: The route url.
|
|
400
|
-
- **String** `method`: The HTTP method.
|
|
401
|
-
- **Function** `cb`: The callback function.
|
|
402
|
-
- **Number** `transType`: The transformer type.
|
|
403
|
-
|
|
404
|
-
### `after(url, method, cb, transType)`
|
|
405
|
-
Adds a before hook. It will handle all the subroutes of the `url`.
|
|
406
|
-
|
|
407
|
-
#### Params
|
|
408
|
-
|
|
409
|
-
- **String** `url`: The route url.
|
|
410
|
-
- **String** `method`: The HTTP method.
|
|
411
|
-
- **Function** `cb`: The callback function.
|
|
412
|
-
- **Number** `transType`: The transformer type.
|
|
413
|
-
|
|
414
|
-
### `use(url, method, cb, transType)`
|
|
415
|
-
Use this function to add middleware handlers.
|
|
416
|
-
|
|
417
|
-
#### Params
|
|
141
|
+
## :question: Get Help
|
|
418
142
|
|
|
419
|
-
|
|
420
|
-
- **String** `method`: The HTTP method.
|
|
421
|
-
- **Function** `cb`: The callback function.
|
|
422
|
-
- **Number** `transType`: The transformer type.
|
|
143
|
+
There are few ways to get help:
|
|
423
144
|
|
|
424
|
-
### `add(url, method, cb)`
|
|
425
|
-
Adds a new middleware. **Note**: This will *not* trigger the hooks.
|
|
426
145
|
|
|
427
|
-
#### Params
|
|
428
146
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
147
|
+
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
148
|
+
2. For bug reports and feature requests, open issues. :bug:
|
|
149
|
+
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
432
150
|
|
|
433
151
|
|
|
434
152
|
|
|
@@ -480,31 +198,6 @@ Thanks! :heart:
|
|
|
480
198
|
|
|
481
199
|
|
|
482
200
|
|
|
483
|
-
## :dizzy: Where is this library used?
|
|
484
|
-
If you are using this library in one of your projects, add it in this list. :sparkles:
|
|
485
|
-
|
|
486
|
-
- `@dottjt/mediareade`
|
|
487
|
-
- `@everything-registry/sub-chunk-2069`
|
|
488
|
-
- `bloggify`
|
|
489
|
-
- `bloggify-cli`
|
|
490
|
-
- `bloggify-prebuilt`
|
|
491
|
-
- `bloggify-server-prebuilt`
|
|
492
|
-
- `bnotify`
|
|
493
|
-
- `chewb-youtube-uploader`
|
|
494
|
-
- `fb-token-updater`
|
|
495
|
-
- `gh-contributions`
|
|
496
|
-
- `learning-node-file-upload`
|
|
497
|
-
- `learning-nodejs-file-upload`
|
|
498
|
-
- `lien-github-comments-example`
|
|
499
|
-
- `nodejs-youtube-uploader`
|
|
500
|
-
- `test-youtube-api`
|
|
501
|
-
- `web-term`
|
|
502
|
-
- `wrabbit`
|
|
503
|
-
- `youtube-album-uploader`
|
|
504
|
-
- `youtube-album-uploader-multiple`
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
201
|
|
|
509
202
|
|
|
510
203
|
|
package/assets/logo.png
ADDED
|
Binary file
|