Skip to content

Instantly share code, notes, and snippets.

@mcroydon
Created July 22, 2010 05:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcroydon/485609 to your computer and use it in GitHub Desktop.
Save mcroydon/485609 to your computer and use it in GitHub Desktop.
// gopher.js - a minimal gopher implementation using node.js
// Released under the 3 clause BSD license by Matt Croydon <mcroydon@gmail.com> (http://postneo.com)
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.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 {
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