-
Notifications
You must be signed in to change notification settings - Fork 6
/
cors.js
57 lines (40 loc) · 995 Bytes
/
cors.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
'use strict'
/**
* Module dependencies
*/
const express = require('express')
const fs = require('fs')
const path = require('path')
/**
* To test, publish something on 'test-cors' channel
*
* $ redis-cli publish test-cors testmessage
*/
// stream
const subscribe = require('..')
// express app
let frontend = express()
let backend = express()
frontend.get('/', (req, res) => {
var index = path.join(__dirname, './cors.html')
fs.createReadStream(index).pipe(res)
})
backend.get('/stream', (req, res) => {
var sse = subscribe({
channels: 'test-cors'
})
req.socket.setTimeout(Number.MAX_VALUE)
res.set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
})
sse.pipe(res)
})
frontend.listen(3000, () => {
console.log('Express (frontend) listening on port 3000')
})
backend.listen(4000, function() {
console.log('Express (backend) listening on port 4000')
})