-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathindex.js
41 lines (35 loc) · 1.41 KB
/
index.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
/**
* Module dependencies.
*/
const express = require('express');
const { createProxyMiddleware } = require('../../dist'); // require('http-proxy-middleware');
/**
* Configure proxy middleware
*/
const wsProxy = createProxyMiddleware({
target: 'http://ws.ifelse.io',
// pathRewrite: {
// '^/websocket' : '/socket', // rewrite path.
// '^/removepath' : '' // remove path.
// },
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
ws: true, // enable websocket proxy
logLevel: 'debug',
});
const app = express();
app.use('/', express.static(__dirname)); // demo page
app.use(wsProxy); // add the proxy to express
const server = app.listen(3000);
server.on('upgrade', wsProxy.upgrade); // optional: upgrade externally
console.log('[DEMO] Server: listening on port 3000');
console.log('[DEMO] Opening: http://localhost:3000');
require('open')('http://localhost:3000');
/**
* Example:
* Open http://localhost:3000 in WebSocket compatible browser.
* In browser console:
* 1. `const socket = new WebSocket('ws://localhost:3000');` // create new WebSocket
* 2. `socket.onmessage = function (msg) {console.log(msg)};` // listen to socket messages
* 3. `socket.send('hello world');` // send message
* > {data: "hello world"} // server should echo back your message.
**/