fastify
Version:
Fast and low overhead web framework, for Node.js
115 lines (93 loc) • 3.56 kB
Markdown
<h1 align="center">Fastify</h1>
<a name="server"></a>
`fastify.server`: The Node core [server](https://nodejs.org/api/http.html#http_class_http_server) object
<a name="ready"></a>
Function called when all the plugins has been loaded.
It takes an error parameter if something went wrong.
```js
fastify.ready(err => {
if (err) throw err
})
```
<a name="listen"></a>
Starts the server on the given port after all the plugins are loaded, internally waits for the `.ready()` event. The callback is the same as the Node core.
```js
fastify.listen(3000, err => {
if (err) throw err
})
```
Specifying an address is also supported:
```js
fastify.listen(3000, '127.0.0.1', err => {
if (err) throw err
})
```
<a name="route"></a>
Method to add routes to the server, it also have shorthands functions, check [here](https://github.com/fastify/fastify/blob/master/docs/Routes.md).
<a name="routes-iterator"></a>
The Fastify instance is an Iterable object with all the registered routes.
The route properties are the same the developer has declared [here](https://github.com/fastify/fastify/blob/master/docs/Routes.md).
```js
fastify.get('/route', opts, handler)
fastify.ready(() => {
for (var route of fastify) {
console.log(route)
/* will output:
{
'/route': {
get: {
method: String,
ulr: String,
schema: Object,
handler: Function,
Request: Function,
Reply: Function
}
}
}
*/
}
})
```
<a name="close"></a>
`fastify.close(callback)`: call this function to close the server instance and run the [`'onClose'`](https://github.com/fastify/fastify/blob/master/docs/Hooks.md#on-close) hook.
<a name="decorate"></a>
Function useful if you need to decorate the fastify instance, Reply or Request, check [here](https://github.com/fastify/fastify/blob/master/docs/Decorators.md).
<a name="register"></a>
Fastify allows the user to extend its functionalities with plugins.
A plugin can be a set of routes, a server decorator or whatever, check [here](https://github.com/fastify/fastify/blob/master/docs/Plugins.md).
<a name="use"></a>
Function to add middlewares to Fastify, check [here](https://github.com/fastify/fastify/blob/master/docs/Middlewares.md).
<a name="addHook"></a>
Function to add a specific hook in the lifecycle of Fastify, check [here](https://github.com/fastify/fastify/blob/master/docs/Hooks.md).
<a name="logger"></a>
The logger instance, check [here](https://github.com/fastify/fastify/blob/master/docs/Logging.md).
<a name="inject"></a>
Fake http injection (for testing purposes) [here](https://github.com/fastify/fastify/blob/master/docs/Testing.md#inject).
<a name="set-schema-compiler"></a>
Set the schema compiler for all routes [here](https://github.com/fastify/fastify/blob/master/docs/Validation-And-Serialize.md#schema-compiler).
<a name="set-not-found-handler"></a>
`fastify.setNotFoundHandler(handler(request, reply))`: set the 404 handler.
This call is fully encapsulated, so different plugins can set different
not found handlers.
<a name="set-error-handler"></a>
`fastify.setErrorHandler(handler(error, reply))`: set a function that
will be called whenever an error happens. The handler is fully
encapsulated, so different plugins can set different
error handlers.