Hi all,
Just wondering if this is any better.
I've tried to implement what Martin has suggested best to my knowledge.
Any further feedback is appreciated.
Thanks
// Set up the app
var express = require('express')
, app = express()
, fs = require('fs')
, path = require('path');
// Define the path to the templates and set it to 'dir'
var url = path.resolve('./templates/');
// Get all files from the template dir and send them to readFile() to be read
var readDir = function(){
fs.readdir(url, function (err,files){
if (err) {
console.log(err.message);
return;
}
// for each of the files...
files.forEach(function (file) {
var filePath = path.join(url, file);
// Get a files stats
fs.stat(filePath, function (err, stat) {
// If the file is not a directory, read it
if (stat && stat.isFile()) {
fs.readFile(filePath, 'utf8', function (err,data) {
if (err) {
console.log(err.message);
return;
}
console.log('read this ' + data);
});
}
});
});
// Assuming the readdir() is complete, lets call templatesLoaded();
templatesLoaded(err);
});
};
// when readdir() is complete, call templatesLoaded()
var templatesLoaded = function (err) {
// Check err and bail out if something bad happened.
if (err) {
console.log(err.message);
return;
}
app.listen(3001);
console.log('Listening on port 3000');
};
// Default route
app.get('/', function(req, res){
res.send('Styleguide');
});
// Call readDir();
readDir();