BVB Source Codes

mongo-express Show app.js Source code

Return Download mongo-express: download app.js Source code - Download mongo-express Source code - Type:.js
  1. #!/usr/bin/env node
  2.  
  3. 'use strict';
  4.  
  5. const clc             = require('cli-color');
  6. const csrf            = require('csurf');
  7. const commander       = require('commander');
  8. const express         = require('express');
  9. const fs              = require('fs');
  10. const https           = require('https');
  11. const middleware      = require('./lib/middleware');
  12. const utils           = require('./lib/utils');
  13. const updateNotifier  = require('update-notifier');
  14. const pkg             = require('./package.json');
  15.  
  16. let app               = express();
  17. let notifier          = updateNotifier({ pkg });
  18.  
  19. let config;
  20. let defaultPort = 80;
  21. let server      = app;
  22. let sslOptions;
  23.  
  24. // Notify of any updates
  25. notifier.notify();
  26.  
  27. try {
  28.   // eslint-disable-next-line import/no-unresolved
  29.   config = utils.deepmerge(require('./config.default'), require('./config'));
  30. } catch (e) {
  31.   if (e.code === 'MODULE_NOT_FOUND') {
  32.     console.log('No custom config.js found, loading config.default.js');
  33.   } else {
  34.     console.error(clc.red('Unable to load config.js!'));
  35.     console.error(clc.red('Error is:'));
  36.     console.log(clc.red(e));
  37.     process.exit(1);
  38.   }
  39.  
  40.   config = require('./config.default');
  41. }
  42.  
  43. if (config.options.console) {
  44.   console.log('Welcome to mongo-express');
  45.   console.log('------------------------');
  46.   console.log('\n');
  47. }
  48.  
  49. commander
  50.   .version(require('./package').version)
  51.   .option('-H, --host <host>', 'hostname or adress')
  52.   .option('-P, --dbport <host>', 'port of the db')
  53.   .option('-u, --username <username>', 'username for authentication')
  54.   .option('-p, --password <password>', 'password for authentication')
  55.   .option('-a, --admin', 'enable authentication as admin')
  56.   .option('-d, --database <database>', 'authenticate to database')
  57.   .option('--port <port>', 'listen on specified port')
  58. .parse(process.argv);
  59.  
  60. if (commander.username && commander.password) {
  61.   config.mongodb.admin = !!commander.admin;
  62.   if (commander.admin) {
  63.     config.mongodb.adminUsername = commander.username;
  64.     config.mongodb.adminPassword = commander.password;
  65.   } else {
  66.     let user = {
  67.       database: commander.database,
  68.       username: commander.username,
  69.       password: commander.password,
  70.     };
  71.     for (let key in user) {
  72.       if (!user[key]) {
  73.         commander.help();
  74.       }
  75.     }
  76.  
  77.     config.mongodb.auth[0] = user;
  78.   }
  79.  
  80.   config.useBasicAuth = false;
  81. }
  82.  
  83. config.mongodb.server = commander.host || config.mongodb.server;
  84. config.mongodb.port = commander.dbport || config.mongodb.port;
  85.  
  86. config.site.port = commander.port || config.site.port;
  87.  
  88. if (!config.site.baseUrl) {
  89.   console.error('Please specify a baseUrl in your config. Using "/" for now.');
  90.   config.site.baseUrl = '/';
  91. }
  92.  
  93. app.use(config.site.baseUrl, middleware(config));
  94. app.use(csrf());
  95.  
  96. if (config.site.sslEnabled) {
  97.   defaultPort     = 443;
  98.   sslOptions  = {
  99.     key:  fs.readFileSync(config.site.sslKey),
  100.     cert: fs.readFileSync(config.site.sslCert),
  101.   };
  102.   server = https.createServer(sslOptions, app);
  103. }
  104.  
  105. let addressString = (config.site.sslEnabled ? 'https://' : 'http://') + (config.site.host || '0.0.0.0') + ':' + (config.site.port || defaultPort);
  106.  
  107. server.listen(config.site.port, config.site.host, function () {
  108.   if (config.options.console) {
  109.  
  110.     console.log('Mongo Express server listening', 'at ' + addressString);
  111.  
  112.     if (!config.site.host || config.site.host === '0.0.0.0') {
  113.       console.error(clc.red('Server is open to allow connections from anyone (0.0.0.0)'));
  114.     }
  115.  
  116.     if (config.basicAuth.username === 'admin' && config.basicAuth.password === 'pass') {
  117.       console.error(clc.red('basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!'));
  118.     }
  119.  
  120.   }
  121. })
  122. .on('error', function (e) {
  123.   if (e.code === 'EADDRINUSE') {
  124.     console.log();
  125.     console.error(clc.red('Address ' + addressString + ' already in use! You need to pick a different host and/or port.'));
  126.     console.log('Maybe mongo-express is already running?');
  127.   }
  128.  
  129.   console.log();
  130.   console.log('If you are still having trouble, try Googling for the key parts of the following error object before posting an issue');
  131.   console.log(JSON.stringify(e));
  132.   return process.exit(1);
  133. });
  134.  
downloadapp.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top