From 032a9b6701a5db6ee3c7564b994fef1f9460adb1 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Thu, 12 Aug 2021 12:13:00 +0200 Subject: [PATCH 1/3] docs: add esm syntax --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index da5c0f71d1..71e7874459 100644 --- a/README.md +++ b/README.md @@ -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 }) @@ -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 }) From 778212b817d7dea04af41625576efd0413be2ee3 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Thu, 12 Aug 2021 12:48:19 +0200 Subject: [PATCH 2/3] docs: add esm syntax --- docs/Getting-Started.md | 63 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/docs/Getting-Started.md b/docs/Getting-Started.md index 7a8135d865..3db673e5e7 100644 --- a/docs/Getting-Started.md +++ b/docs/Getting-Started.md @@ -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 }) @@ -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.
*(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 }) @@ -89,6 +102,12 @@ As with JavaScript, where everything is an object, with Fastify everything is a Before digging into it, let's see how it works!
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' +const fastify = Fastify({ + logger: true +}) +// CommonJs const fastify = require('fastify')({ logger: true }) @@ -132,6 +151,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 }) @@ -144,13 +185,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) { From 04aaa9b807db5e21573b4905790b78b234c86fa0 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Thu, 12 Aug 2021 12:57:11 +0200 Subject: [PATCH 3/3] docs: add esm syntax --- docs/Getting-Started.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/Getting-Started.md b/docs/Getting-Started.md index 3db673e5e7..5cf43ec090 100644 --- a/docs/Getting-Started.md +++ b/docs/Getting-Started.md @@ -104,9 +104,23 @@ Let's declare our basic server, but instead of declaring the route inside the en ```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 @@ -119,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} }) ```