Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getting started #462

Merged
merged 1 commit into from Nov 14, 2017
Merged

Improve getting started #462

merged 1 commit into from Nov 14, 2017

Conversation

delvedor
Copy link
Member

Fastify has different concepts that are not present in the other frameworks and our getting started section is failing to show them and give a gentle introduction to the framework.
This pr aims to fix that, please review it :)

@delvedor delvedor added documentation Improvements or additions to documentation enhancement labels Nov 13, 2017
```js
const fastify = require('fastify')()

fastify.register(require('./our-db-connector'), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should note somewhere that there is fastify-mongodb

<a name="schema"></a>
### Schema serialization
Fastify is designed for performance. To truly turbocharge our server, we can serialize responses according to the [JSON Schema](http://json-schema.org/) standard:
Do you prefer to use `async/await`? We got you covered.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not finished reviewing, and will surely find more changes to request. But it's time for me to head to lunch and I wanted to give you some feedback while you still have time to consider it and incorporate it given our timezone difference. So I'm submitting halfway through my review.

@@ -1,13 +1,18 @@
<h1 align="center">Fastify</h1>

## Getting Started
Hello! Thank you for checking out Fastify!
This document aims to be a gentle introduction to the framework and its features. You will find some easy explanation, examples and link to other parts of the documentation if you want to get deepen in some argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This document aims to be a gentle introduction to the framework and its features. You will find some easy explanation, It is an elementary introduction with examples and links to other parts of the documentation. if you want to get deepen in some argument.

<a name="schema"></a>
### Schema serialization
Fastify is designed for performance. To truly turbocharge our server, we can serialize responses according to the [JSON Schema](http://json-schema.org/) standard:
Do you prefer to use `async/await`? We got you covered.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer to use async/await? We got you covered Fastify supports it out-of-the-box.

### Schema serialization
Fastify is designed for performance. To truly turbocharge our server, we can serialize responses according to the [JSON Schema](http://json-schema.org/) standard:
Do you prefer to use `async/await`? We got you covered.
*(we also suggest to use [make-promises-safe](https://github.com/mcollina/make-promises-safe) to avoid file descriptor and memory leaks)*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we also suggest to use using make-promises-safe to avoid file descriptor and memory leaks)

// Declare a route with an output schema
fastify.get('/', opts, function (request, reply) {
reply.send({ hello: 'world' })
fastify.get('/', async (request, reply) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the reply parameter even need to be supplied?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if you need to set some custom header o change the status code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe a note about it and why it isn't being used here would be good to have.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a note could be a nice idea, but I think that this document should provide just a getting started, we have provided the link to other parts of the documentation that explains better the usage of every api. I don't think that add here too many info could be a good idea.

@@ -56,61 +44,167 @@ fastify.listen(3000, function (err) {
})
```

*To learn more about using serialization and validation, see [Validation and Serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md)!*
Awesome, that was easy.
Unfortunately, write a complex application requires definitely more code than what we wrote above. A classic problem when you are building a new application is how handle multiple files, asynchronous bootstrapping and the architecture of your code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, write writing a complex application requires definitely significantly more code than what we wrote above this example.

### Install
```
npm i fastify --save
```
<a name="first-server"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> <a id="first-server"></a>

<a name="register"></a>
### Register
As you can see, using Fastify is very easy and handy. Obviously, registering all of your routes in the same file is not a good idea, so Fastify offers a utility to solve this problem, `register`:
<a name="first-plugin"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> <a id="first-plugin"></a>

As you can see, using Fastify is very easy and handy. Obviously, registering all of your routes in the same file is not a good idea, so Fastify offers a utility to solve this problem, `register`:
<a name="first-plugin"></a>
### Your first plugin
As in javascript everything is an object, in Fastify everything is a plugin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in javascript with Javascript where everything is an object, in with Fastify everything is a plugin.

### Your first plugin
As in javascript everything is an object, in Fastify everything is a plugin.
Before digging into it, let's see how it works!
Let's declare our basic server, but instead of declare the route inside the entry point, we'll declare it in an external file (checkout the [route declaration](https://github.com/fastify/fastify/blob/master/docs/Routes.md) docs).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's declare our basic server, but instead of declare declaring the route inside the entry point, we'll declare it in an external file (checkout the route declaration docs).

if (err) throw err
module.exports = routes
```
As you can see we used the `register` api, that you will use a lot when working with Fastify, since is the only way to register routes, plugins and so on.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see In this example we used the register api API. , that you will use a lot when working with Fastify, since is the only way to register routes, plugins and so on. This API is the core of the Fastify framework, and is the only way to register routes, plugins and so on.

@delvedor
Copy link
Member Author

@jsumners I have addressed almost all your suggestions, thanks!

if (err) throw err
module.exports = routes
```
In this we used the `register` API. This API is the core of the Fastify framework, and is the only way to register routes, plugins and so on.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this [example] we used...

```
In this we used the `register` API. This API is the core of the Fastify framework, and is the only way to register routes, plugins and so on.

At the beginning of this document we said that Fastify helps you on handling the asynchronous bootstrapping of your application. Why this is important?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the beginning of this document guide we said noted that Fastify helps you on handling provides a foundation that assists with the asynchronous bootstrapping of your an application. Why this is important?

In this we used the `register` API. This API is the core of the Fastify framework, and is the only way to register routes, plugins and so on.

At the beginning of this document we said that Fastify helps you on handling the asynchronous bootstrapping of your application. Why this is important?
Well, let's say you must connect to database to fetch some data, obviously you don't want that the server starts accept requests before the database connection has been established. How can you address this problem?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, let's say you must connect to database to fetch some data, obviously you don't want that the server starts accept requests before the database connection has been established. How can you address this problem?

Consider the scenario where a database connection is needed to handle data storage. Obviously the database connection needs to be available prior to the server accepting connections. How do we address this problem?


At the beginning of this document we said that Fastify helps you on handling the asynchronous bootstrapping of your application. Why this is important?
Well, let's say you must connect to database to fetch some data, obviously you don't want that the server starts accept requests before the database connection has been established. How can you address this problem?
Usually you will work with some complex callback/promises system that will mix the framework api with other libraries and your code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually you will work with some complex callback/promises system that will mix the framework api with other libraries and your code.

A typical solution is to use a complex callback, or promises, system that will mix the framework API with other libraries and the application code.

At the beginning of this document we said that Fastify helps you on handling the asynchronous bootstrapping of your application. Why this is important?
Well, let's say you must connect to database to fetch some data, obviously you don't want that the server starts accept requests before the database connection has been established. How can you address this problem?
Usually you will work with some complex callback/promises system that will mix the framework api with other libraries and your code.
Fastify handles this internally for you, with the minimum effort!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fastify handles this internally for you, with the minimum effort!

<a name="extend-server"></a>
### Extend your server
Fastify is built to be extremely extensible and it's very minimal, we believe that provide a fully featured framework is not needed, since it will probably give you more than what you need.
In other words, Fastify is not batteries included and if you need them, checkout our amazing [ecosystem](https://github.com/fastify/fastify/blob/master/docs/Ecosystem.md)!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, Fastify is not batteries included a "batteries included" framework, and if you need them, checkout our relies on an amazing ecosystem!


<a name="test-server"></a>
### Test your server
We not offer a testing framework, but we do recommend a way to write your test that uses in a smart way the features and the architecture of Fastify.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We not Fastify does not offer a testing framework, but we do recommend a way to write your test tests that uses in a smart way the features and the architecture of Fastify.

<a name="test-server"></a>
### Test your server
We not offer a testing framework, but we do recommend a way to write your test that uses in a smart way the features and the architecture of Fastify.
Checkout the [testing](https://github.com/fastify/fastify/blob/master/docs/Testing.md) documentation!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout the testing documentation!

Read the testing documentation to learn more.

### Run from CLI
You can also run Fastify from the CLI thanks to [fastify-cli](https://github.com/fastify/fastify-cli). It's very easy! Simply add the following lines to your `package.json`:
### Run your server from CLI
You can also run Fastify from the CLI thanks to [fastify-cli](https://github.com/fastify/fastify-cli). It's very easy!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also run Fastify also has from the CLI integration thanks to fastify-cli. It's very easy!

You can also run Fastify from the CLI thanks to [fastify-cli](https://github.com/fastify/fastify-cli). It's very easy! Simply add the following lines to your `package.json`:
### Run your server from CLI
You can also run Fastify from the CLI thanks to [fastify-cli](https://github.com/fastify/fastify-cli). It's very easy!
Simply add the following lines to your `package.json`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply add the following lines to your package.json:

Copy link
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work (as always)!


fastify.listen(8000, function (err) {
fastify.listen(3000, function (err) {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fastify.log.info('...')?

### Schema serialization
Fastify is designed for performance. To truly turbocharge our server, we can serialize responses according to the [JSON Schema](http://json-schema.org/) standard:
Do you prefer to use `async/await`? Fastify supports it out-of-the-box.
*(we also suggest using [make-promises-safe](https://github.com/mcollina/make-promises-safe) to avoid file descriptor and memory leaks)*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is out of the context. Maybe a recommendation is better for this tips

@@ -56,61 +44,167 @@ fastify.listen(3000, function (err) {
})
```

*To learn more about using serialization and validation, see [Validation and Serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md)!*
Awesome, that was easy.
Unfortunately, writing a complex application requires significantly more code than this example. A classic problem when you are building a new application is how handle multiple files, asynchronous bootstrapping and the architecture of your code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new application is how handle multiple files => a new application is how handling multiple files

*To learn more about using serialization and validation, see [Validation and Serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md)!*
Awesome, that was easy.
Unfortunately, writing a complex application requires significantly more code than this example. A classic problem when you are building a new application is how handle multiple files, asynchronous bootstrapping and the architecture of your code.
Fastify offers an easy platform that helps solve all of problems, and more.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that helps solve => that helps solving

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or add a "these" between "of" and "problems".

something: true
fastify.listen(3000, function (err) {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fastify.log.info ?

Fastify is built to be extremely extensible and it's very minimal, we believe that provide a fully featured framework is not needed, since it will probably give you more than what you need.
In other words, Fastify is not batteries included and if you need them, checkout our amazing [ecosystem](https://github.com/fastify/fastify/blob/master/docs/Ecosystem.md)!

<a name="test-server"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> <a id="test-server"></a>

### Test your server
We not offer a testing framework, but we do recommend a way to write your test that uses in a smart way the features and the architecture of Fastify.
Checkout the [testing](https://github.com/fastify/fastify/blob/master/docs/Testing.md) documentation!

<a name="cli"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=><a id="cli"></a>

@@ -271,6 +270,26 @@ module.exports = fp(dbPlugin)
```
You can also tell to `fastify-plugin` to check the installed version of Fastify, in case of you need a specific api.

<a name="handle-errors"></a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> <a id="handle-errors"></a>

<a name="url-building"></a>
### Url building
Fastify supports both static and dynamic urls.
To register a **parametric** path, use the *colon* before the parameter name. For **wildcard** use the *star*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> To register a **parametric** path, use the *colon* (`:`) before the parameter name. For **wildcard** use the *star*` (`*`)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed, there is an example few line below and everybody know what is a column and what is a star :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"star" => "asterisk"

docs/Routes.md Outdated
In this case as parameter separator it's possible to use whatever character is not matched by the regular expression.

Having a route with multiple parameters may affect negatively the performance, so prefer single parameter approach whenever possible, especially on routes which are on the hot path of your application.
If you are interested hon how we handle the routing, checkout [find-my-way](https://github.com/delvedor/find-my-way).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> If you are interested in

@delvedor
Copy link
Member Author

I've addressed almost all suggestions, please take a final look :)

@delvedor delvedor merged commit 2b962a8 into master Nov 14, 2017
@delvedor delvedor deleted the improve-getting-started branch November 14, 2017 15:28
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 23, 2022
@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation semver-minor Issue or PR that should land as semver minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants