// Original file created by Prof.Dr. Matthias Hopf /* * Common functions and imports */ var common = { fs: require('fs'), // file sync http: require('http'), mongoose: require('mongoose'), // Needed for db connection. //util: require('util'), // Why is it needed here? //fork: require('child_process') .fork, // What does that? // Generate Error object suitible for throwing or next()ing // For a better exception handling genError: function (code, message) { var err = new Error (common.http.STATUS_CODES[code] + (message != undefined && message != "" ? ": "+message : "")); err.status = code; // to generally disable stack traces for these manually created error Objects: delete err.stack; return err; }, // Generate deep copy // Only include properties incl (all if undefined), strip properties excl (associative arrays) deepCopy: function (inp, incl, excl) { // For now, JSON is considered fastest / easiest var obj = JSON.parse (JSON.stringify (inp)); if (incl) { for (var k in obj) { if (incl[k] === undefined) delete obj[k]; } } if (excl) { for (var k in excl) { delete obj[k]; } } return obj; }, // Create shallow (1 level) copy of object, use obj if already present (merge) // Only include properties incl (all if undefined), strip properties excl (associative arrays) shallowCopy: function (inp, incl, excl, obj) { var keys = inp; if (obj === undefined) obj = {}; if (typeof inp == "array") obj = []; if (incl !== undefined) keys = incl; for (var k in keys) { if (inp[k] !== undefined && (excl === undefined || ! excl[k])) obj[k] = inp[k]; } return obj; }, // Create hash of 'true' entries for array/mongoose object arrayToHash: function (array) { var hash = {}; for (var e=0; e < array.length; e++) { hash[array[e]] = true; } return hash; }, // Log output session cookie debug: function (req) { console.log ("- " + req.headers.cookie + "\n+ " + req.session.id + "\n " + JSON.stringify (req.session)); }, // Init config data init: function () { this.config = JSON.parse (this.fs.readFileSync ("server_config.json")); }, }; module.exports = common;