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

Docs: esm syntax #3253

Merged
merged 3 commits into from Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -84,6 +84,13 @@ yarn add fastify

```js
// Require the framework and instantiate it

// ESM
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand All @@ -103,6 +110,12 @@ fastify.listen(3000, (err, address) => {
with async-await:

```js
// ESM
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand Down
79 changes: 76 additions & 3 deletions docs/Getting-Started.md
Expand Up @@ -21,6 +21,13 @@ yarn add fastify
Let's write our first server:
```js
// Require the framework and instantiate it

// ESM
Copy link
Member

Choose a reason for hiding this comment

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

Just to share, would it be more straightforward for users get a table

esm cjs
code code

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Won't this break the doc website? I will be hard to read on all devices

Copy link
Member

Choose a reason for hiding this comment

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

Just for curiosity, how the website would looks like with:

ESM
import Fastify from 'fastify'
const fastify = Fastify({
  logger: true
})
Commonjs
const fastify = require('fastify')({
  logger: true
})

?

import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand All @@ -36,13 +43,19 @@ fastify.listen(3000, function (err, address) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
// Server is now listening on ${address}
})
```

Do you prefer to use `async/await`? Fastify supports it out-of-the-box.<br>
*(We also suggest using [make-promises-safe](https://github.com/mcollina/make-promises-safe) to avoid file descriptor and memory leaks.)*
```js
// ESM
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand Down Expand Up @@ -89,6 +102,26 @@ As with JavaScript, where everything is an object, with Fastify everything is a
Before digging into it, let's see how it works!<br>
Let's declare our basic server, but instead of declaring the route inside the entry point, we'll declare it in an external file (check out the [route declaration](Routes.md) docs).
```js
// ESM
import Fastify from 'fastify'
import firstRoute from './our-first-route'
const fastify = Fastify({
logger: true
})

fastify.register(firstRoute)

fastify.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})
```

```js
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand All @@ -100,7 +133,7 @@ fastify.listen(3000, function (err, address) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
// Server is now listening on ${address}
})
```

Expand Down Expand Up @@ -132,6 +165,28 @@ npm i --save fastify-plugin fastify-mongodb

**server.js**
```js
// ESM
import Fastify from 'fastify'
import dbConnector from './our-db-connector'
import firstRoute from './our-first-route'

const fastify = Fastify({
logger: true
})
fastify.register(dbConnector)
fastify.register(firstRoute)

fastify.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})
```

```js
// CommonJs
const fastify = require('fastify')({
logger: true
})
Expand All @@ -144,13 +199,31 @@ fastify.listen(3000, function (err, address) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
// Server is now listening on ${address}
})

```

**our-db-connector.js**
```js
// ESM
import fastifyPlugin from 'fastify-plugin'
import fastifyMongo from 'fastify-mongodb'

async function dbConnector (fastify, options) {
fastify.register(fastifyMongo, {
url: 'mongodb://localhost:27017/test_database'
})
}

// Wrapping a plugin function with fastify-plugin exposes the decorators
// and hooks, declared inside the plugin to the parent scope.
module.exports = fastifyPlugin(dbConnector)

```

```js
// CommonJs
const fastifyPlugin = require('fastify-plugin')

async function dbConnector (fastify, options) {
Expand Down