This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 351
Feature/fastify examples hello world #25
Open
ghinks
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
ghinks:feature/fastify-examples-hello-world
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8ff355
docs: fastify hello world
ghinks 78bc030
feature: fastify/hello-world
ghinks eec8336
feature: fastify/hello-world
ghinks 104d7a5
feature: fastify/hello-world
ghinks 698108a
Update servers/fastify/hello-world/README.md
ghinks fc48a73
feature: fastify/hello-world
ghinks a6434ce
feature: fastify/hello-world
ghinks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,4 +116,5 @@ dist | |
|
||
# ignore lockfiles | ||
package-lock.json | ||
yarn.lock | ||
yarn.lock | ||
./servers/fastify/hello-world/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
servers/fastify/hello-world/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -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}`) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This log line should not be needed, this info is logged by fastify itself now |
||||
} catch (err) { | ||||
fastify.log.error(err) | ||||
process.exit(1) | ||||
} | ||||
} | ||||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log line should not be needed, this info is logged by fastify itself now