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/.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/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 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 new file mode 100644 index 00000000..c3baa80b --- /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](./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"} +``` + +## Testing + +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 + +```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' } +}) + +fastify.listen(APP_PORT) +``` + +**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 }) +``` + +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 +fastify.listen(APP_PORT) +``` + +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 +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 +seconds timeout. + +```javascript +fastify.get('/', async (request, reply) => { + await setTimeoutPromise(2000) + reply.send({ + "hello": "world" + }) +}) +``` + +Whats going on here? The route handler sets a timer to simulate asynchronous behavior. In addition the call to fastify +is provided with 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 new file mode 100644 index 00000000..716b1551 --- /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) + reply.send({ + "hello": "world" + }) +}) + +// 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.js b/servers/fastify/hello-world/hello-world.js new file mode 100644 index 00000000..3f4eb4a7 --- /dev/null +++ b/servers/fastify/hello-world/hello-world.js @@ -0,0 +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' } +}) + +fastify.listen(APP_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" + } +}