Skip to content

Commit

Permalink
Added a check for the existence of the images directory, and an argum…
Browse files Browse the repository at this point in the history
…ent to set the images directory
  • Loading branch information
dalewebster48 committed Feb 24, 2017
1 parent 4851c54 commit 89273b4
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions lib/ever-layout-bridge.js
@@ -1,12 +1,26 @@
var http = require('http');
var fs = require("fs");
var url = require("url");
var args = require("minimist")(process.argv.splice(2));

// Directory of layout files
var layoutDirectory = args.layouts;
if (! layoutDirectory) layoutDirectory = "Layouts";

// Directory of images
var imageDirectory = args.images
if (! imageDirectory) imageDirectory = "Images"

// Listening port
var port = args.port;
if (! port) port = 3000;

var server = http.createServer(function(req , res){
var request = url.parse(req.url , true);
var action = request.pathname;
console.log(action);

var imagePath = "images" + action;
var imagePath = imageDirectory + action;
if (fs.existsSync(imagePath)) {
var image = fs.readFileSync("images" + action);
console.log("Serving image " + action);
Expand All @@ -17,23 +31,19 @@ var server = http.createServer(function(req , res){
}
});

var socket = require("socket.io")(server);
var args = require("minimist")(process.argv.splice(2));

// Directory of layout files
var directory = args.dir;
if (! directory) directory = "Layouts";

// Listening port
var port = args.port;
if (! port) port = 3000;

// Make sure this directory exists
if (fs.existsSync(directory) == false) {
console.log("Could not find directory: " + directory);
if (fs.existsSync(layoutDirectory) == false) {
console.log("Could not find directory: " + layoutDirectory);
process.exit();
}

if (fs.existsSync(imageDirectory) == false) {
console.log("Could not find the images directory: " + imageDirectory);
console.log("Images cannot be served.")
}

// Create sockets
var socket = require("socket.io")(server);
socket.on("connection" , function(client){
console.log("Connected");

Expand All @@ -51,8 +61,8 @@ socket.on("connection" , function(client){
});

// Watch the layouts directory for changes
fs.watch(directory , function(event , filename){
fs.readFile(directory + "/" + filename , function(err , data){
fs.watch(layoutDirectory , function(event , filename){
fs.readFile(layoutDirectory + "/" + filename , function(err , data){
if (err) return console.log("There was an error");
console.log("Serving " + filename);
// Parse the JSON from the file
Expand Down

0 comments on commit 89273b4

Please sign in to comment.