You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.4 KiB
58 lines
1.4 KiB
4 years ago
|
'use strict'
|
||
|
|
||
|
const fastify = require('../fastify')()
|
||
|
|
||
|
const opts = {
|
||
|
schema: {
|
||
|
response: {
|
||
|
200: {
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
hello: {
|
||
|
type: 'string'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fastify
|
||
|
.get('/', opts, function (req, reply) {
|
||
|
reply.send({ hello: 'world' })
|
||
|
})
|
||
|
.get('/promise', opts, function (req, reply) {
|
||
|
const promise = new Promise(function (resolve, reject) {
|
||
|
resolve({ hello: 'world' })
|
||
|
})
|
||
|
reply.header('content-type', 'application/json').code(200).send(promise)
|
||
|
})
|
||
|
.get('/return-promise', opts, function (req, reply) {
|
||
|
const promise = new Promise(function (resolve, reject) {
|
||
|
resolve({ hello: 'world' })
|
||
|
})
|
||
|
return promise
|
||
|
})
|
||
|
.get('/stream', function (req, reply) {
|
||
|
const fs = require('fs')
|
||
|
const stream = fs.createReadStream(process.cwd() + '/examples/plugin.js', 'utf8')
|
||
|
reply.code(200).send(stream)
|
||
|
})
|
||
|
.post('/', opts, function (req, reply) {
|
||
|
reply.send({ hello: 'world' })
|
||
|
})
|
||
|
.head('/', {}, function (req, reply) {
|
||
|
reply.send()
|
||
|
})
|
||
|
.delete('/', opts, function (req, reply) {
|
||
|
reply.send({ hello: 'world' })
|
||
|
})
|
||
|
.patch('/', opts, function (req, reply) {
|
||
|
reply.send({ hello: 'world' })
|
||
|
})
|
||
|
|
||
|
fastify.listen(3000, err => {
|
||
|
if (err) throw err
|
||
|
console.log(`server listening on ${fastify.server.address().port}`)
|
||
|
})
|