1.1.0 • Published 3 years ago
@awesomeorganization/sse-handler v1.1.0
sse-handler
:boom: ESM The server-sent events (sse) handler for Node.js according to w3c and whatwg
Example
Full example in /example
folder.
import { http } from '@awesomeorganization/servers'
import { rewriteHandler } from '@awesomeorganization/rewrite-handler'
import { sseHandler } from '@awesomeorganization/sse-handler'
import { staticHandler } from '@awesomeorganization/static-handler'
const example = async () => {
const rewriteMiddleware = rewriteHandler({
rules: [
{
pattern: '(.*)/$',
replacement: '$1/index.html',
},
],
})
const sseMidleware = sseHandler()
const staticMiddleware = await staticHandler({
directoryPath: './static',
})
http({
listenOptions: {
host: '127.0.0.1',
port: 3000,
},
onListening() {
setInterval(() => {
const timestamp = new Date().toISOString()
sseMidleware.push({
data: `${timestamp}: Hi!`,
})
sseMidleware.push({
data: [timestamp, 'This is multiline', 'string with event.'].join('\n'),
event: 'someEvent',
})
}, 3e3)
},
onRequest(request, response) {
switch (request.method) {
case 'GET': {
switch (request.url) {
case '/sse': {
sseMidleware.handle({
request,
response,
})
return
}
default: {
rewriteMiddleware.handle({
request,
response,
})
staticMiddleware.handle({
request,
response,
})
return
}
}
}
}
response.end()
},
})
// TRY
// http://127.0.0.1:3000/
}
example()
1.1.0
3 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago
0.2.7
4 years ago
0.2.6
4 years ago
0.2.5
4 years ago
0.2.4
4 years ago
0.2.1
4 years ago
0.2.3
4 years ago
0.2.2
4 years ago
0.2.0
4 years ago
0.1.9
4 years ago
0.1.8
4 years ago
0.1.7
4 years ago
0.1.6
4 years ago
0.1.5
4 years ago
0.1.4
4 years ago
0.1.3
4 years ago
0.1.2
4 years ago
0.1.1
5 years ago
0.1.0
5 years ago