Skip to content

Commit

Permalink
Docs: esm syntax (#3253)
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth committed Aug 16, 2021
1 parent e0541c7 commit aee28e8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
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
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

0 comments on commit aee28e8

Please sign in to comment.