server.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //User Defined Modules
  2. var config = require('./config');
  3. var logger = require('./utils/logger');
  4. //Low-Level Modules
  5. var ip = require('ip');
  6. var minimist = require('minimist')(process.argv.slice(2));
  7. //ExpressJS (Server-Side) Modules
  8. var express = require('express');
  9. var app = express();
  10. var bodyParser = require('body-parser');
  11. //MongoDB Modules
  12. var mongoose = require('mongoose');
  13. // =========================================== START SETUP ========================================================== //
  14. //Set the port for the server
  15. var port = process.env.PORT || config.httpPort;
  16. // ========================================= EXPRESS ======================================================== //
  17. //Configure app to use bodyParser()
  18. //Allows for parsing information from POST requests
  19. app.use(bodyParser.urlencoded({extended: true}));
  20. app.use(bodyParser.json());
  21. //Tell the app to use the router config we defined in /routes
  22. //In this case, every route is prefixed with '/api/'
  23. //This could be anything, but considering it's an API operation, this prefix should be suitable
  24. app.use('/api', require('./routes/index'));
  25. // ====================================== CMD LINE ARGS ===================================================== //
  26. //Check if the prod flag was set (-p or --prod)
  27. var prod = false;
  28. if (minimist.p || minimist.prod) {
  29. prod = true;
  30. app.use(express.static('dist'));
  31. }
  32. else {
  33. app.use(express.static('app'));
  34. }
  35. //Check if the test flag was set (-t or --test)
  36. var test = false;
  37. if (minimist.t || minimist.test) {
  38. test = true;
  39. /*
  40. WARNING!!!
  41. The following code tells NodeJS to ignore ALL HTTPS security (SSL/TLS).
  42. This is OK for testing, but should NEVER be enabled in production.
  43. Since the server was started with the -t flag, we are running on a test version.
  44. */
  45. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  46. }
  47. // ========================================= MONGODB ======================================================== //
  48. var dbOptions = {
  49. user: config.db.username,
  50. pass: config.db.password,
  51. auth: {
  52. authdb: 'admin'
  53. }
  54. };
  55. mongoose.connect(config.db.tools.path, dbOptions);
  56. mongoose.connection.on('error', function () {
  57. logger.error("ERROR CONNECTING TO DB. Check that the DB is online.");
  58. process.exit(1);
  59. });
  60. // ============================================ END SETUP =========================================================== //
  61. app.listen(port);
  62. console.log('Server is running on port: ' + port);