From f8ff355c7f78c8ff742d98a076f80a5f300ae9c8 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Sat, 15 Aug 2020 10:21:17 -0400 Subject: [PATCH 1/7] docs: fastify hello world A very basic example of a base route for the fastify framework. More verbose descriptions are given as hello world examples are often read by folks new to eco system. --- package.json | 1 + servers/fastify/hello-world/README.md | 131 ++++++++++++++++++ .../fastify/hello-world/hello-world-async.js | 25 ++++ servers/fastify/hello-world/hello-world.js | 19 +++ 4 files changed, 176 insertions(+) create mode 100644 servers/fastify/hello-world/README.md create mode 100644 servers/fastify/hello-world/hello-world-async.js create mode 100644 servers/fastify/hello-world/hello-world.js diff --git a/package.json b/package.json index 618e41ab..f020712c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "standard": "^14.3.3" }, "dependencies": { + "fastify": "^3.2.0", "yargs": "^15.3.1" }, "standard": { diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md new file mode 100644 index 00000000..a93bd4ac --- /dev/null +++ b/servers/fastify/hello-world/README.md @@ -0,0 +1,131 @@ +# Fastify Hello World +All the code examples are compliant with the [standard](https://standardjs.com/index.html) linting style. This hello +world example goes into more detail than subsequent examples as it is intended for possible newbies. + +## Instructions +Run the fastify **hello-world.js** file with nodeJS at the command line. + +``` +node hello-world.js +``` + +Fastify is being configured with logging turned on and you should immediately see logs similar to + +``` +{"level":30,"time":1597497138242,"pid":49826,"hostname":"mymachineName.local","msg":"Server listening at http://127.0.0.1:3000"} +{"level":30,"time":1597497138243,"pid":49826,"hostname":"mymachineName.local","msg":"server listening on 3000"} +``` + +## Testing + +Either use [curl](https://curl.haxx.se/) on the command line + +``` + curl http://localhost:3000 +``` + +or paste this into the browser of your choice + +``` +http://localhost:3000/ +``` + +you should get the hello world response. The server is responding on the root path with a JSON object + +```json +{ + "hello": "world" +} +``` + +The format of response will vary depending on your browser and installed plugins. + +## Description + +Lets look at what is going on here. + +```javascript +// Require the framework and instantiate it +const fastify = require('fastify')({ logger: true }) + +// Declare a route +fastify.get('/', async (request, reply) => { + return { hello: 'world' } +}) + +// Run the server! +const start = async () => { + try { + await fastify.listen(3000) + fastify.log.info(`server listening on ${fastify.server.address().port}`) + } catch (err) { + fastify.log.error(err) + process.exit(1) + } +} +start() +``` + +**Fastify** is required into the application and called immediately. + +```javascript +const fastify = require('fastify')({ logger: true }) +``` + +This could equally be written as +```javascript +const fastifyServer = require('fastify'); +const fastify = fastifyServer({ + logger: true +}) +``` + +The next thing is a **route** is declared. + +```javascript +fastify.get('/', async (request, reply) => { + return { hello: 'world' } +}) +``` + +This is adding a route to base path '/' that will handle **get** requests on that path. The handling function takes two arguements. +These are [request](https://www.fastify.io/docs/latest/Request/) and [reply](https://www.fastify.io/docs/latest/Reply/). +Note that the reply object is simply returned and that the handling function is declared as **async** + +Lets see how the server is being started + +```javascript +const start = async () => { + try { + await fastify.listen(3000) + fastify.log.info(`server listening on ${fastify.server.address().port}`) + } catch (err) { + fastify.log.error(err) + process.exit(1) + } +} +start() +``` + +The **start** function is created. Note it is an asynchronous function. The **listen** function on fastify is called +with a port number of **3000**. +The **start** function once called will cause **fastify** to listen on port 3000 for requests to the base route '/'. + +## Hello World, with an asynchronous response + +The hello-world.js example responded synchronously but what if the reply could not be made synchronously and depended +on other asynchronous services. +To simulate an asynchronous response the [hello-world-async.js](./hello-world-async.js) route uses a timer with a 2 +second timeout. + +```javascript +fastify.get('/', async (request, reply) => { + await setTimeoutPromise(2000).then(() => { + reply.send({ + "hello": "world" + }) + }) +}) +``` + +Whats going on here? The route handler sets a timer to simulate asychronous behavior. diff --git a/servers/fastify/hello-world/hello-world-async.js b/servers/fastify/hello-world/hello-world-async.js new file mode 100644 index 00000000..c5aa6f12 --- /dev/null +++ b/servers/fastify/hello-world/hello-world-async.js @@ -0,0 +1,25 @@ +// Require the framework and instantiate it +const util = require('util') +const setTimeoutPromise = util.promisify(setTimeout) +const fastify = require('fastify')({ logger: true }) + +// Declare a route +fastify.get('/', async (request, reply) => { + await setTimeoutPromise(2000).then(() => { + reply.send({ + "hello": "world" + }) + }) +}) + +// Run the server! +const start = async () => { + try { + await fastify.listen(3000) + fastify.log.info(`server listening on ${fastify.server.address().port}`) + } catch (err) { + fastify.log.error(err) + process.exit(1) + } +} +start() diff --git a/servers/fastify/hello-world/hello-world.js b/servers/fastify/hello-world/hello-world.js new file mode 100644 index 00000000..730741e0 --- /dev/null +++ b/servers/fastify/hello-world/hello-world.js @@ -0,0 +1,19 @@ +// Require the framework and instantiate it +const fastify = require('fastify')({ logger: true }) + +// Declare a route +fastify.get('/', async (request, reply) => { + return { hello: 'world' } +}) + +// Run the server! +const start = async () => { + try { + await fastify.listen(3000) + fastify.log.info(`server listening on ${fastify.server.address().port}`) + } catch (err) { + fastify.log.error(err) + process.exit(1) + } +} +start() From 78bc0304849f2c687672d6747489146526350413 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Mon, 17 Aug 2020 20:14:25 -0400 Subject: [PATCH 2/7] feature: fastify/hello-world basic hello world in fastify --- package.json | 1 - servers/fastify/hello-world/README.md | 53 +++++++++---------- .../fastify/hello-world/hello-world-async.js | 1 + servers/fastify/hello-world/hello-world.js | 13 +---- servers/fastify/hello-world/package.json | 15 ++++++ 5 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 servers/fastify/hello-world/package.json diff --git a/package.json b/package.json index f020712c..618e41ab 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "standard": "^14.3.3" }, "dependencies": { - "fastify": "^3.2.0", "yargs": "^15.3.1" }, "standard": { diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md index a93bd4ac..2a440793 100644 --- a/servers/fastify/hello-world/README.md +++ b/servers/fastify/hello-world/README.md @@ -3,7 +3,7 @@ All the code examples are compliant with the [standard](https://standardjs.com/i world example goes into more detail than subsequent examples as it is intended for possible newbies. ## Instructions -Run the fastify **hello-world.js** file with nodeJS at the command line. +Run the fastify [hello-world.js](./hello-world.js) file with nodeJS at the command line. ``` node hello-world.js @@ -53,20 +53,11 @@ fastify.get('/', async (request, reply) => { return { hello: 'world' } }) -// Run the server! -const start = async () => { - try { - await fastify.listen(3000) - fastify.log.info(`server listening on ${fastify.server.address().port}`) - } catch (err) { - fastify.log.error(err) - process.exit(1) - } -} -start() +fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) ``` -**Fastify** is required into the application and called immediately. +**Fastify** is required into the application and called immediately with a configuration object. The object sets fastify's +logging to true. ```javascript const fastify = require('fastify')({ logger: true }) @@ -95,21 +86,11 @@ Note that the reply object is simply returned and that the handling function is Lets see how the server is being started ```javascript -const start = async () => { - try { - await fastify.listen(3000) - fastify.log.info(`server listening on ${fastify.server.address().port}`) - } catch (err) { - fastify.log.error(err) - process.exit(1) - } -} -start() +fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) ``` -The **start** function is created. Note it is an asynchronous function. The **listen** function on fastify is called -with a port number of **3000**. -The **start** function once called will cause **fastify** to listen on port 3000 for requests to the base route '/'. +The **listen** function is called upon fastify and provided with a port number and a callback. + ## Hello World, with an asynchronous response @@ -128,4 +109,22 @@ fastify.get('/', async (request, reply) => { }) ``` -Whats going on here? The route handler sets a timer to simulate asychronous behavior. +Whats going on here? The route handler sets a timer to simulate asynchronous behavior. In addition the call to fastify +is provided no callback. When no callback is given fastify returns a promise. We are now starting fastify within an +asynchronous function. + +```javascript + +// Run the server! +const start = async () => { + try { + // if no callback is give a promise is returned + await fastify.listen(3000) + fastify.log.info(`server listening on ${fastify.server.address().port}`) + } catch (err) { + fastify.log.error(err) + process.exit(1) + } +} +start() +``` diff --git a/servers/fastify/hello-world/hello-world-async.js b/servers/fastify/hello-world/hello-world-async.js index c5aa6f12..680f0a72 100644 --- a/servers/fastify/hello-world/hello-world-async.js +++ b/servers/fastify/hello-world/hello-world-async.js @@ -15,6 +15,7 @@ fastify.get('/', async (request, reply) => { // Run the server! const start = async () => { try { + // if no callback is give a promise is returned await fastify.listen(3000) fastify.log.info(`server listening on ${fastify.server.address().port}`) } catch (err) { diff --git a/servers/fastify/hello-world/hello-world.js b/servers/fastify/hello-world/hello-world.js index 730741e0..f564a13e 100644 --- a/servers/fastify/hello-world/hello-world.js +++ b/servers/fastify/hello-world/hello-world.js @@ -1,19 +1,10 @@ // Require the framework and instantiate it const fastify = require('fastify')({ logger: true }) +const APP_PORT = 3000 // Declare a route fastify.get('/', async (request, reply) => { return { hello: 'world' } }) -// Run the server! -const start = async () => { - try { - await fastify.listen(3000) - fastify.log.info(`server listening on ${fastify.server.address().port}`) - } catch (err) { - fastify.log.error(err) - process.exit(1) - } -} -start() +fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) diff --git a/servers/fastify/hello-world/package.json b/servers/fastify/hello-world/package.json new file mode 100644 index 00000000..049214f4 --- /dev/null +++ b/servers/fastify/hello-world/package.json @@ -0,0 +1,15 @@ +{ + "name": "hello-world", + "version": "1.0.0", + "description": "All the code examples are compliant with the [standard](https://standardjs.com/index.html) linting style. This hello world example goes into more detail than subsequent examples as it is intended for possible newbies.", + "main": "hello-world.js", + "scripts": { + "start": "node hello-world.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "dependencies": { + "fastify": "^3.2.1" + } +} From eec8336996d9c82a840e37c8fd9e461873614501 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Wed, 19 Aug 2020 14:45:26 -0400 Subject: [PATCH 3/7] feature: fastify/hello-world markdown lint corrections A markdownlintignore file has been added as there will be multiple folders which contain node module readmes that do not comply. --- .gitignore | 3 ++- .markdownlintignore | 2 ++ package.json | 2 +- servers/fastify/hello-world/README.md | 24 +++++++++++++----------- 4 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 .markdownlintignore diff --git a/.gitignore b/.gitignore index ef1a04ad..96528a03 100644 --- a/.gitignore +++ b/.gitignore @@ -116,4 +116,5 @@ dist # ignore lockfiles package-lock.json -yarn.lock \ No newline at end of file +yarn.lock +./servers/fastify/hello-world/node_modules diff --git a/.markdownlintignore b/.markdownlintignore new file mode 100644 index 00000000..fbf67e07 --- /dev/null +++ b/.markdownlintignore @@ -0,0 +1,2 @@ +node_modules +servers/fastify/hello-world/node_modules diff --git a/package.json b/package.json index 618e41ab..6015038b 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test:unit": "jest --coverage", "test:onlychanged": "jest --onlyChanged --coverage", - "test:markdown": "markdownlint . --ignore ./node_modules", + "test:markdown": "markdownlint . ", "lint": "standard", "test": "npm run lint && npm run test:unit && npm run test:markdown" }, diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md index 2a440793..4fb96d75 100644 --- a/servers/fastify/hello-world/README.md +++ b/servers/fastify/hello-world/README.md @@ -1,17 +1,19 @@ # Fastify Hello World + All the code examples are compliant with the [standard](https://standardjs.com/index.html) linting style. This hello world example goes into more detail than subsequent examples as it is intended for possible newbies. ## Instructions + Run the fastify [hello-world.js](./hello-world.js) file with nodeJS at the command line. -``` +```shell script node hello-world.js ``` Fastify is being configured with logging turned on and you should immediately see logs similar to -``` +```text {"level":30,"time":1597497138242,"pid":49826,"hostname":"mymachineName.local","msg":"Server listening at http://127.0.0.1:3000"} {"level":30,"time":1597497138243,"pid":49826,"hostname":"mymachineName.local","msg":"server listening on 3000"} ``` @@ -20,17 +22,17 @@ Fastify is being configured with logging turned on and you should immediately se Either use [curl](https://curl.haxx.se/) on the command line -``` +```shell script curl http://localhost:3000 ``` or paste this into the browser of your choice -``` +```shell script http://localhost:3000/ ``` -you should get the hello world response. The server is responding on the root path with a JSON object +you should get the hello world response. The server is responding on the root path with a JSON object ```json { @@ -40,7 +42,7 @@ you should get the hello world response. The server is responding on the root pa The format of response will vary depending on your browser and installed plugins. -## Description +## Description Lets look at what is going on here. @@ -63,7 +65,8 @@ logging to true. const fastify = require('fastify')({ logger: true }) ``` -This could equally be written as +This could equally be written as + ```javascript const fastifyServer = require('fastify'); const fastify = fastifyServer({ @@ -71,7 +74,7 @@ const fastify = fastifyServer({ }) ``` -The next thing is a **route** is declared. +The next thing is a **route** is declared. ```javascript fastify.get('/', async (request, reply) => { @@ -80,7 +83,7 @@ fastify.get('/', async (request, reply) => { ``` This is adding a route to base path '/' that will handle **get** requests on that path. The handling function takes two arguements. -These are [request](https://www.fastify.io/docs/latest/Request/) and [reply](https://www.fastify.io/docs/latest/Reply/). +These are [request](https://www.fastify.io/docs/latest/Request/) and [reply](https://www.fastify.io/docs/latest/Reply/). Note that the reply object is simply returned and that the handling function is declared as **async** Lets see how the server is being started @@ -91,7 +94,6 @@ fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.s The **listen** function is called upon fastify and provided with a port number and a callback. - ## Hello World, with an asynchronous response The hello-world.js example responded synchronously but what if the reply could not be made synchronously and depended @@ -110,7 +112,7 @@ fastify.get('/', async (request, reply) => { ``` Whats going on here? The route handler sets a timer to simulate asynchronous behavior. In addition the call to fastify -is provided no callback. When no callback is given fastify returns a promise. We are now starting fastify within an +is provided no callback. When no callback is given fastify returns a promise. We are now starting fastify within an asynchronous function. ```javascript From 104d7a53fd0c5b84acc2fcc68f7231223c6fa45c Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Wed, 19 Aug 2020 15:12:20 -0400 Subject: [PATCH 4/7] feature: fastify/hello-world add contributor ghinks --- .all-contributorsrc | 9 +++++++++ README.md | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 8a1b0c96..4199dcdc 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -31,6 +31,15 @@ "infra", "ideas" ] + }, + { + "login": "ghinks", + "name": "Glenn", + "avatar_url": "https://avatars3.githubusercontent.com/u/5049078?v=4", + "profile": "http://glennhinks.com/", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7 diff --git a/README.md b/README.md index 00730ac6..1d4ffde6 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,12 @@ We've documented how to meaningfully contribute in [CONTRIBUTING.md](./CONTRIBUT - - + + +

Tierney Cyren

💻 🖋 📖 ⚠️

Matteo Collina

🚇 🤔

Tierney Cyren

💻 🖋 📖 ⚠️

Matteo Collina

🚇 🤔

Glenn

💻
- + \ No newline at end of file From 698108a30fccab6cc0666aaa1faf6878dccb2ba6 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 20 Aug 2020 15:42:37 -0400 Subject: [PATCH 5/7] Update servers/fastify/hello-world/README.md Co-authored-by: Rodion Abdurakhimov --- servers/fastify/hello-world/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md index 4fb96d75..18c91582 100644 --- a/servers/fastify/hello-world/README.md +++ b/servers/fastify/hello-world/README.md @@ -99,7 +99,7 @@ The **listen** function is called upon fastify and provided with a port number a The hello-world.js example responded synchronously but what if the reply could not be made synchronously and depended on other asynchronous services. To simulate an asynchronous response the [hello-world-async.js](./hello-world-async.js) route uses a timer with a 2 -second timeout. +seconds timeout. ```javascript fastify.get('/', async (request, reply) => { From fc48a73ebe5652e1f54a06849c97d8ea0ef7a43b Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Thu, 20 Aug 2020 15:46:50 -0400 Subject: [PATCH 6/7] feature: fastify/hello-world quickly fix embarrassing promise abuse in code and README.md --- servers/fastify/hello-world/README.md | 11 +++++------ servers/fastify/hello-world/hello-world-async.js | 7 +++---- servers/fastify/hello-world/hello-world.js | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md index 18c91582..b8b58817 100644 --- a/servers/fastify/hello-world/README.md +++ b/servers/fastify/hello-world/README.md @@ -55,7 +55,7 @@ fastify.get('/', async (request, reply) => { return { hello: 'world' } }) -fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) +fastify.listen(APP_PORT) ``` **Fastify** is required into the application and called immediately with a configuration object. The object sets fastify's @@ -89,7 +89,7 @@ Note that the reply object is simply returned and that the handling function is Lets see how the server is being started ```javascript -fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) +fastify.listen(APP_PORT) ``` The **listen** function is called upon fastify and provided with a port number and a callback. @@ -103,10 +103,9 @@ seconds timeout. ```javascript fastify.get('/', async (request, reply) => { - await setTimeoutPromise(2000).then(() => { - reply.send({ - "hello": "world" - }) + await setTimeoutPromise(2000) + reply.send({ + "hello": "world" }) }) ``` diff --git a/servers/fastify/hello-world/hello-world-async.js b/servers/fastify/hello-world/hello-world-async.js index 680f0a72..716b1551 100644 --- a/servers/fastify/hello-world/hello-world-async.js +++ b/servers/fastify/hello-world/hello-world-async.js @@ -5,10 +5,9 @@ const fastify = require('fastify')({ logger: true }) // Declare a route fastify.get('/', async (request, reply) => { - await setTimeoutPromise(2000).then(() => { - reply.send({ - "hello": "world" - }) + await setTimeoutPromise(2000) + reply.send({ + "hello": "world" }) }) diff --git a/servers/fastify/hello-world/hello-world.js b/servers/fastify/hello-world/hello-world.js index f564a13e..3f4eb4a7 100644 --- a/servers/fastify/hello-world/hello-world.js +++ b/servers/fastify/hello-world/hello-world.js @@ -7,4 +7,4 @@ fastify.get('/', async (request, reply) => { return { hello: 'world' } }) -fastify.listen(APP_PORT, () => fastify.log.info(`server listening on ${fastify.server.address().port}`)) +fastify.listen(APP_PORT) From a6434ce4c2499b4b4312a68700670a1e172f88b4 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Thu, 20 Aug 2020 15:49:28 -0400 Subject: [PATCH 7/7] feature: fastify/hello-world change language as suggested by @rodion-arr --- servers/fastify/hello-world/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servers/fastify/hello-world/README.md b/servers/fastify/hello-world/README.md index b8b58817..c3baa80b 100644 --- a/servers/fastify/hello-world/README.md +++ b/servers/fastify/hello-world/README.md @@ -111,7 +111,7 @@ fastify.get('/', async (request, reply) => { ``` Whats going on here? The route handler sets a timer to simulate asynchronous behavior. In addition the call to fastify -is provided no callback. When no callback is given fastify returns a promise. We are now starting fastify within an +is provided with no callback. When no callback is given fastify returns a promise. We are now starting fastify within an asynchronous function. ```javascript