Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Original file created by Prof.Dr. Matthias Hopf
  2. /*
  3. * Common functions and imports
  4. */
  5. var common = {
  6. fs: require('fs'), // file sync
  7. http: require('http'),
  8. mongoose: require('mongoose'), // Needed for db connection.
  9. //util: require('util'), // Why is it needed here?
  10. //fork: require('child_process') .fork, // What does that?
  11. // Generate Error object suitible for throwing or next()ing
  12. // For a better exception handling
  13. genError: function (code, message) {
  14. var err = new Error (common.http.STATUS_CODES[code] + (message != undefined && message != "" ? ": "+message : ""));
  15. err.status = code;
  16. // to generally disable stack traces for these manually created error Objects:
  17. delete err.stack;
  18. return err;
  19. },
  20. // Generate deep copy
  21. // Only include properties incl (all if undefined), strip properties excl (associative arrays)
  22. deepCopy: function (inp, incl, excl) {
  23. // For now, JSON is considered fastest / easiest
  24. var obj = JSON.parse (JSON.stringify (inp));
  25. if (incl) {
  26. for (var k in obj) {
  27. if (incl[k] === undefined)
  28. delete obj[k];
  29. }
  30. }
  31. if (excl) {
  32. for (var k in excl) {
  33. delete obj[k];
  34. }
  35. }
  36. return obj;
  37. },
  38. // Create shallow (1 level) copy of object, use obj if already present (merge)
  39. // Only include properties incl (all if undefined), strip properties excl (associative arrays)
  40. shallowCopy: function (inp, incl, excl, obj) {
  41. var keys = inp;
  42. if (obj === undefined)
  43. obj = {};
  44. if (typeof inp == "array")
  45. obj = [];
  46. if (incl !== undefined)
  47. keys = incl;
  48. for (var k in keys) {
  49. if (inp[k] !== undefined && (excl === undefined || ! excl[k]))
  50. obj[k] = inp[k];
  51. }
  52. return obj;
  53. },
  54. // Create hash of 'true' entries for array/mongoose object
  55. arrayToHash: function (array) {
  56. var hash = {};
  57. for (var e=0; e < array.length; e++) {
  58. hash[array[e]] = true;
  59. }
  60. return hash;
  61. },
  62. // Log output session cookie
  63. debug: function (req) {
  64. console.log ("- " + req.headers.cookie + "\n+ " + req.session.id + "\n " + JSON.stringify (req.session));
  65. },
  66. // Init config data
  67. init: function () {
  68. this.config = JSON.parse (this.fs.readFileSync ("server_config.json"));
  69. },
  70. };
  71. module.exports = common;