Hi Everyone, I'm a long time reader, first time poster.
I'm wondering if you guys could provide some feedback on this code I've
written, for my first attempt at an app.
My intention is to read the contents of files from a specific directory.
So when you load this 'app' it iterates all the files in 'templates' and
sends them to my readFile() function to be read.
This script works as I expected, but I'd like to see how some of your more
experienced developers would have attempted this.
Thanks
Chris
// Set up the app
var express = require('express')
, app = express()
, fs = require('fs');
// Define the path to the templates and set it to 'dir'
app.set('templates', __dirname + '/templates');
var dir = app.get('templates');
// Get all files from the template dir and
// send em to readFile() to be read
var readDir = function(){
fs.readdir(dir, function (err,files){
if(err) throw err;
files.forEach(function(file) {
readFile(file);
});
});
};
// Read the files
var readFile = function (file) {
fs.readFile(dir+'/'+file, 'utf8', function (err,data) {
if(err) throw err;
console.log("read this " + data);
});
};
// Default route
app.get('/', function(req, res){
readDir();
res.send('Styleguide');
});
// App Listen
app.listen(3000);
console.log('Listening on port 3000');
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en