Is it safe to do something like this in Node.js/Express.js?
// use Object.create(null) so we don't have to worry about key collisons
// see http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
var theHash = Object.create(null);
exports.store = function (req, res) {
key = getUniqueKey();
theHash[key] = req.param('val');
// finish the request, &c.
}
exports.retrieve = function (req, res) {
res.end(theHash[req.param('key')]);
}
Basically, I’m building a short-lived short URL service, and this seemed like an easy and fast way to do it. Items are removed from the hash every 24 hours so it can’t get to large. Is this safe, or do I need to use a database of some sort?
If you want tips (as per comments), then yes, this is poor design, but not specifically wrong.
You would be better suited to use Redis for this, an in memory key=>value store. What you have done isn’t wrong though, but you rightly notice that it feels “dirty” to keep your keys locally( for several reasons, but crucially if node dies, you lose everything).