I started writing a node application and I want to store request and response objects in a hash table. For a hash table, I use jshashtable . When I store request and response objects in a hash table and get them later, I get an Object.keys called on non-object error when I try to use response , whether writeHead() or just print using console.log() . However, typeof returns an object for response , so it turns out that the response processed when it is stored in jshashtable . On the jshashtable website jshashtable author writes: “Objects are used freely here to mean any object or JavaScript value.” So it seems like I should be able to store any javascript object, including a response object.
jshashtable can be installed using npm install jshashtable .
Here is some code that replicates the problem.
var Hashtable = require('jshashtable'); var crypto = require('crypto'); var table = new Hashtable(); var http = require('http'); var fs = require("fs"); var home = fs.readFileSync('/some/random/html/home.html'); http.createServer(function(req, res) { crypto.randomBytes(8, function(ex, buf) { if (ex) throw ex; var userID = buf.toString('hex'); var state = { "req": req, "res": res } table.put(userID, state); var message = { "httpCode": 200, "humanCode": "OK", "contentType": "text/html", "data": home }; dataOut(userID, message, function(err, rtrn) { }); }); }).listen(80); function dataOut(userID, message, callback) { if(typeof callback === 'function') { var state; state = table.get(userID); if(state === null) { console.log("Can't get value"); callback("Can't get value from key.", null); } if(typeof state.res === 'object') { console.log('This is an object'); }
So why can't I use request and response after storing them in jshashtable ?
source share