Skip to content

Instantly share code, notes, and snippets.

@emceeaich
Forked from mcroydon/gopher.js
Last active August 26, 2016 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emceeaich/a3f73673c512582feb4e33c3831b815d to your computer and use it in GitHub Desktop.
Save emceeaich/a3f73673c512582feb4e33c3831b815d to your computer and use it in GitHub Desktop.
// gopher.js - a gopher implementation using node.js
// Released under the 3 clause BSD license by Matt Croydon <mcroydon@gmail.com> (http://postneo.com)
// Forked by Emma Humphries (https://emmah.net/) for stupid Internet of Things tricks
var net = require('net');
net.createServer(function (socket) {
socket.setEncoding("ascii");
socket.on("data", function (data) {
if (data === '\r\n') {
console.log('Serving index.');
socket.write('0About gopher.js' + '\t' + 'About' + '\t' + '127.0.0.1' + '\t' + '70');
socket.write('.\r\n');
socket.write('0Local Temperature' + '\t' + 'Temp' + '\t' + '127.0.0.1' + '\t' + '70');
socket.write('.\r\n');
socket.end();
}
else if (data === 'About\r\n') {
console.log('Serving about file.');
socket.write('Gopher.js is a minimal implementation of the gopher protocol using node.js.\r\n' +
'To try it for yourself, run "node gopher.js" and connect to gopher://127.0.0.1 using a gopher client or a browser like Firefox.\r\n' +
'You will likely need to run this command as root or with sudo.\r\n');
socket.end();
}
else if (data === 'Temp\r\n') {
console.log('Serving climate module data');
socket.write('Local climate data here\r\n');
socket.end();
}
else {
console.log('Unknown: ' + data);
}
});
socket.on("end", function () {
console.log('Ending connection.');
socket.end();
});
}).listen(70, "127.0.0.1");
console.log('Server running at 127.0.0.1:70');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment