Chapter 11: update example microservice
This commit is contained in:
parent
791aac76d7
commit
cb0e52e541
4
Chapter11/fastify-microservice/.gitignore
vendored
4
Chapter11/fastify-microservice/.gitignore
vendored
@ -56,7 +56,3 @@ profile-*
|
||||
profile*
|
||||
*clinic*
|
||||
*flamegraph*
|
||||
|
||||
# generated code
|
||||
*/**/*.js
|
||||
dist
|
||||
|
||||
@ -16,11 +16,11 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"fastify": "^3.0.0",
|
||||
"fastify-plugin": "^2.0.0",
|
||||
"fastify-plugin": "^3.0.0",
|
||||
"fastify-autoload": "^3.0.2",
|
||||
"fastify-cli": "^2.0.2"
|
||||
"fastify-cli": "^2.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": "^14.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,6 @@ that will then be used in the rest of your application.
|
||||
|
||||
Check out:
|
||||
|
||||
* [The hitchhiker's guide to plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md)
|
||||
* [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Plugins-Guide/)
|
||||
* [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
|
||||
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).
|
||||
|
||||
12
Chapter11/fastify-microservice/plugins/support.js
Normal file
12
Chapter11/fastify-microservice/plugins/support.js
Normal file
@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const fp = require('fastify-plugin')
|
||||
|
||||
// the use of fastify-plugin is required to be able
|
||||
// to export the decorators to the outer scope
|
||||
|
||||
module.exports = fp(async function (fastify, opts) {
|
||||
fastify.decorate('someSupport', function () {
|
||||
return 'hugs'
|
||||
})
|
||||
})
|
||||
@ -23,4 +23,5 @@ If you need to share functionality between services, place that
|
||||
functionality into the `plugins` folder, and share it via
|
||||
[decorators](https://www.fastify.io/docs/latest/Decorators/).
|
||||
|
||||
If you have a bit confused about using `async/await` write services, you would better take a look at [Promise resolution](https://github.com/fastify/fastify/blob/master/docs/Routes.md#promise-resolution) for more details.
|
||||
If you're a bit confused about using `async/await` to write services, you would
|
||||
better take a look at [Promise resolution](https://www.fastify.io/docs/latest/Routes/#promise-resolution) for more details.
|
||||
|
||||
7
Chapter11/fastify-microservice/routes/example/index.js
Normal file
7
Chapter11/fastify-microservice/routes/example/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = async function (fastify, opts) {
|
||||
fastify.get('/', async function (request, reply) {
|
||||
return 'this is an example'
|
||||
})
|
||||
}
|
||||
7
Chapter11/fastify-microservice/routes/root.js
Normal file
7
Chapter11/fastify-microservice/routes/root.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = async function (fastify, opts) {
|
||||
fastify.get('/', async function (request, reply) {
|
||||
return { root: true }
|
||||
})
|
||||
}
|
||||
34
Chapter11/fastify-microservice/test/helper.js
Normal file
34
Chapter11/fastify-microservice/test/helper.js
Normal file
@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
// This file contains code that we reuse
|
||||
// between our tests.
|
||||
|
||||
const Fastify = require('fastify')
|
||||
const fp = require('fastify-plugin')
|
||||
const App = require('../app')
|
||||
|
||||
// Fill in this config with all the configurations
|
||||
// needed for testing the application
|
||||
function config () {
|
||||
return {}
|
||||
}
|
||||
|
||||
// automatically build and tear down our instance
|
||||
function build (t) {
|
||||
const app = Fastify()
|
||||
|
||||
// fastify-plugin ensures that all decorators
|
||||
// are exposed for testing purposes, this is
|
||||
// different from the production setup
|
||||
app.register(fp(App), config())
|
||||
|
||||
// tear down our app after we are done
|
||||
t.tearDown(app.close.bind(app))
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
config,
|
||||
build
|
||||
}
|
||||
26
Chapter11/fastify-microservice/test/plugins/support.test.js
Normal file
26
Chapter11/fastify-microservice/test/plugins/support.test.js
Normal file
@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const Fastify = require('fastify')
|
||||
const Support = require('../../plugins/support')
|
||||
|
||||
test('support works standalone', async (t) => {
|
||||
const fastify = Fastify()
|
||||
fastify.register(Support)
|
||||
|
||||
await fastify.ready()
|
||||
t.equal(fastify.someSupport(), 'hugs')
|
||||
})
|
||||
|
||||
// You can also use plugin with opts in fastify v2
|
||||
//
|
||||
// test('support works standalone', (t) => {
|
||||
// t.plan(2)
|
||||
// const fastify = Fastify()
|
||||
// fastify.register(Support)
|
||||
//
|
||||
// fastify.ready((err) => {
|
||||
// t.error(err)
|
||||
// t.equal(fastify.someSupport(), 'hugs')
|
||||
// })
|
||||
// })
|
||||
27
Chapter11/fastify-microservice/test/routes/example.test.js
Normal file
27
Chapter11/fastify-microservice/test/routes/example.test.js
Normal file
@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { build } = require('../helper')
|
||||
|
||||
test('example is loaded', async (t) => {
|
||||
const app = build(t)
|
||||
|
||||
const res = await app.inject({
|
||||
url: '/example'
|
||||
})
|
||||
t.equal(res.payload, 'this is an example')
|
||||
})
|
||||
|
||||
// inject callback style:
|
||||
//
|
||||
// test('example is loaded', (t) => {
|
||||
// t.plan(2)
|
||||
// const app = build(t)
|
||||
//
|
||||
// app.inject({
|
||||
// url: '/example'
|
||||
// }, (err, res) => {
|
||||
// t.error(err)
|
||||
// t.equal(res.payload, 'this is an example')
|
||||
// })
|
||||
// })
|
||||
27
Chapter11/fastify-microservice/test/routes/root.test.js
Normal file
27
Chapter11/fastify-microservice/test/routes/root.test.js
Normal file
@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { build } = require('../helper')
|
||||
|
||||
test('default root route', async (t) => {
|
||||
const app = build(t)
|
||||
|
||||
const res = await app.inject({
|
||||
url: '/'
|
||||
})
|
||||
t.deepEqual(JSON.parse(res.payload), { root: true })
|
||||
})
|
||||
|
||||
// inject callback style:
|
||||
//
|
||||
// test('default root route', (t) => {
|
||||
// t.plan(2)
|
||||
// const app = build(t)
|
||||
//
|
||||
// app.inject({
|
||||
// url: '/'
|
||||
// }, (err, res) => {
|
||||
// t.error(err)
|
||||
// t.deepEqual(JSON.parse(res.payload), { root: true })
|
||||
// })
|
||||
// })
|
||||
Loading…
x
Reference in New Issue
Block a user