Created
June 8, 2018 13:22
-
-
Save prameshbajra/b8dae4d7a8aa7f989f354ca848fc3afc to your computer and use it in GitHub Desktop.
Tiny api communicator that acts as chat messenger. Made in less than 15 mins. Work locally only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cluster = require('cluster'); | |
if (cluster.isMaster) { | |
cluster.fork(); | |
cluster.on('exit', function(worker, code, signal) { | |
cluster.fork(); | |
}); | |
} | |
if (cluster.isWorker) { | |
const express = require('express'); | |
const event = require('events'); | |
const app = express(); | |
const os = require('os'); | |
const ifaces = os.networkInterfaces(); | |
let msgs = []; | |
let localIP = ""; | |
// To get your local IP such that other can access the file via this ... | |
Object.keys(ifaces).forEach(function (ifname) { | |
let alias = 0; | |
ifaces[ifname].forEach(function (iface) { | |
if ('IPv4' !== iface.family || iface.internal !== false) { | |
return; | |
} | |
localIP = iface.address; | |
++alias; | |
}); | |
}); | |
app.get("/msg/:msg", (req, res) => { | |
const e = new event.EventEmitter(); | |
const msg = req.params.msg; | |
e.on("message", (data) => { | |
console.log(data); | |
msgs.push(data) | |
}) | |
e.emit("message", msg); | |
res.send(msgs); | |
}); | |
app.listen(3000, localIP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment