#!/usr/bin/env node
require("dotenv").config();
var app = require("../app");
var http = require("http");

// Define a port for the server to listen on
var port = 4000;
app.set("port", port);

// Create a server instance
var server = http.createServer(app);

// Make the server listen on a port
server.listen(port);

// Handle errors and success
server.on("error", onError);
server.on("listening", onListening);

/* function pipeOrPort(address) {
	return typeof address == "string" ? `pipe ${address}` : `port ${address.port}`;
} */

function onError(error) {
	if (error.syscall != "listen") {
		throw error;
	}

	let bind = 4000;//pipeOrPort(server.address());

	switch (error.code) {
		case "EACCES":
			console.error(`${bind} requires elevated privileges.`);
			process.exit(1);
		case "EADDRINUSE":
			console.error(`${bind} is already in use.`);
			process.exit(1);
		default:
			throw error;
	}
}

function onListening() {
	let bind = 4000;//pipeOrPort(server.address());
	console.log(`Listening on ${bind}`);
}
