0.0.11 • Published 2 years ago
fwd-server-websocket v0.0.11
fwd-server-websocket
quick and dirty local http server that routes requested urls via websocket to a client that generates responses to the original requests
use ipWhitelistRequesters
and ipWhitelistSocketClients
to limit the IP addresses that can request and generate responses respectively -- remember, any client that connects to the websocket can 'intercept' and generate responses!
this is meant to allow us to use cheap/minimal public-facing servers "free tier" etc while offloading processing for a request to a local machine, without opening ports or directly exposing the local machine.
Installation
npm i fwd-server-websocket
Usage
var fsw = require('fwd-server-websocket');
//defaults shown [except for magicWord]
var port = 3000;
var pollingTimeMs = 10; //poll to wait for response generated by websocket
var magicWord = "hereWeGo"; //default is blank "" -- rejects requests that do not have the magic word in the URL
var contentTypeReturned = "application/json";
//IPs that are allowed to make requests from the server
var ipWhitelistRequesters = ['::ffff:127.0.0.1','::ffff:192.168.0.1','127.0.0.1', '192.168.0.1']; //set to [] to allow ALL ips [not recommended!]
//IPs that are allowed to generate responses for the server
var ipWhitelistSocketClients = ['::ffff:127.0.0.1','::ffff:192.168.0.1','127.0.0.1', '192.168.0.1']; //set to [] to allow ALL ips [not recommended!]
fsw.runServer(port, magicWord, pollingTimeMs, contentTypeReturned, ipWhitelistRequesters, ipWhitelistSocketClients); //run http server on port 3000
//now we can do the following in a different file/ on a different machine etc. or within the same file for this example.
// run websocket client that connects to the server,
// processes incoming msgs from requests, then serves
// the results to be return to the original requester
var defaultMsgRunner = function(message, ws){ //default function echos data wrapped in an object
// Process the received message
console.log('Received message:', message);
const message2 = { messageOrig: JSON.parse(message)};
ws.send(JSON.stringify(message2));
}
var defaultUrl = "ws://localhost:3000";
fsw.runClient(defaultUrl, defaultMsgRunner);
//now we can make a GET request with axios.
// defaultMsgRunner will generate the response!
var axios = require('axios');
//now anytime a GET request to the server is made, the function above is called to process the response
axios.get('http://localhost:3000/hereWeGo_hereIsMyMsg')
.then(response => {
console.log(`Status Code: ${response.status}`); //200
console.log(`Response Body:`,response.data); //{ messageOrig: { data: 'hereWeGo_hereIsMyMsg' } }
})
.catch(error => {
console.error(`An error occurred: ${error}`);
});
//this request gets rejected with a 404 because it is missing the magicWord
axios.get('http://localhost:3000/hereWeDontGo_hereIsMyMsg')
.then(response => {})
.catch(error => {
console.error(`An error occurred: ${error}`); //An error occurred: AxiosError: Request failed with status code 404
});
throw 'done'; //throw error to kill the earlier server, otherwise this example will stay running until we ctrl-c out of it