-
Notifications
You must be signed in to change notification settings - Fork 16
/
example.js
60 lines (48 loc) · 1.08 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use strict";
const fastify = require("fastify")();
const PassThrough = require("stream").PassThrough;
const Fs = require("fs");
fastify
.register(require("./index"))
.after( (err) => {
if (err) {
throw err;
}
}
);
fastify.get("/sse", (request, reply) => {
reply.sse("toto");
setTimeout(() => {
reply.sse({data: "titi au ski", event: "test"});
reply.sse();
}, 500);
});
fastify.get("/sse2", (request, reply) => {
const read = new PassThrough({objectMode: true});
let index = 0;
reply.sse(read);
const id = setInterval(() => {
read.write({event: "test", index});
index += 1;
if (!(index % 10)) {
read.end();
clearInterval(id);
}
}, 1000);
});
fastify.route({
handler: (request, reply) => {
reply.sse(Fs.createReadStream("./package.json"));
},
method: "GET",
url: "/sse3"
});
fastify.get("/", (request, reply) => {
reply.send({hello: "world"});
});
fastify.listen(3000, (err) => {
if (err) {
throw err;
}
console.log(`server listening on ${fastify.server.address().port}`);
});