Created
January 6, 2013 07:01
-
-
Save luin/4465736 to your computer and use it in GitHub Desktop.
A simple fake redis server written in node.
This file contains 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
var net = require('net'); | |
var _dict = {}; | |
// Start a TCP Server | |
net.createServer(function (socket) { | |
socket.write("Welcome to Nodis!\n"); | |
// Handle incoming messages from clients. | |
socket.on('data', function (data) { | |
data = data.toString().replace(/[\r\n]/g, '').split(' '); | |
processCommand(data, socket); | |
}); | |
}).listen(5000); | |
// Put a friendly message on the terminal of the server. | |
console.log("Nodis running at port 5000\n"); | |
function processCommand(argv, socket) { | |
var replyValue; | |
argv[0] = argv[0].toUpperCase(); | |
if (argv[0] === 'SET') { | |
_dict[argv[1]] = argv[2]; | |
socket.write("OK\n"); | |
} else if (argv[0] === 'GET') { | |
replyValue = _dict[argv[1]]; | |
socket.write((replyValue ? replyValue : '(nil)') + "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment