1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import * as fastify from '../fastify'
|
15 | import * as cors from 'cors'
|
16 | import { createReadStream } from 'fs'
|
17 | import * as http from 'http'
|
18 |
|
19 | const server = fastify()
|
20 |
|
21 | const opts = {
|
22 | schema: {
|
23 | response: {
|
24 | 200: {
|
25 | type: 'object',
|
26 | properties: {
|
27 | hello: {
|
28 | type: 'string'
|
29 | }
|
30 | }
|
31 | }
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | function getHelloHandler (req: fastify.FastifyRequest<http.IncomingMessage>,
|
37 | reply: fastify.FastifyReply<http.ServerResponse>) {
|
38 | reply.header('Content-Type', 'application/json').code(200)
|
39 | reply.send({ hello: 'world' })
|
40 | }
|
41 |
|
42 | function getStreamHandler (req, reply) {
|
43 | const stream = createReadStream(process.cwd() + '/examples/plugin.js', 'utf8')
|
44 | reply.code(200).send(stream)
|
45 | }
|
46 |
|
47 | server.use(cors())
|
48 | server.get('/', opts, getHelloHandler)
|
49 | server.get('/stream', getStreamHandler)
|
50 |
|
51 | server.listen(3000, err => {
|
52 | if (err) throw err
|
53 | console.log(`server listening on ${server.server.address().port}`)
|
54 | })
|