Files
TechnicFlux/bin/www

125 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Load configuration from .env
*/
require('dotenv').config()
// Check if the .env config file is included.
const required_variables = [
process.env.MONGODB_CONN_STRING,
process.env.ADMIN_USER,
process.env.ADMIN_PASS,
process.env.NAME,
process.env.HOST,
process.env.API_KEY,
process.env.SESSION_KEY
]
for (let variable of required_variables) {
if (variable === undefined) {
console.error("ERROR: Configuration error! You are either missing a .env file or have not properly configured all " +
"required environment variables.\n" +
"Depending on your environment, this may require copying .env.example to .env and modifying the configuration " +
"file or defining the missing environment variables (see the .env.example file)." +
"\nExiting...")
process.exit(1)
}
}
/**
* Module dependencies.
*/
const app = require('../app').app;
const debug = require('debug')('technicflux:server');
const http = require('http');
const database = require('../database/database')
/**
* Get port from environment and store in Express.
*/
let port = normalizePort(process.env.PORT || '80');
app.set('port', port);
/**
* Create HTTP server.
*/
let server = http.createServer(app);
/**
* Connect to the Database
*/
database.connectToDB(process.env.MONGODB_CONN_STRING)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}