Gold Challenge: Moving Error Handling on its own

fileRead.js:

var fs = require(‘fs’);

var handleError = function(err, res) {
res.writeHead(404);
res.end();
};

var fileRead = function(filePath, res) {
fs.readFile(filePath, function(err, data) {
if(err) {
handleError(err, res);
return false;
} else {
return data;
}
});
};

module.exports = fileRead;

index.js:

var http = require(‘http’);
var extract = require(‘./extract’)
var fileread = require(‘./fileRead’);

var server = http.createServer(function (req, res) {
console.log(‘Responding to a request.’);
var filePath = extract(req.url);
var data = fileread(filePath, res);
if(data) {
res.end(data);
} else {
return;
}
});
server.listen(3000);

What is wrong with this code? I think I am doing a mistake in passing response to the errorhandling module, kindly help.

I got it by doing res.end(data) inside the fileRead function itself rather than returning the value.
Now both the files look like:

index.js:

var http = require(‘http’);
var extract = require(‘./extract’)
var fileread = require(‘./fileRead’);

var server = http.createServer(function (req, res) {
console.log(‘Responding to a request.’);
var filePath = extract(req.url);
fileread(filePath, res);
});
server.listen(3000);

fileRead.js:

var fs = require(‘fs’);

var handleError = function(err, res) {
res.end(“Invalid URL”);
};

var fileRead = function(filePath, res) {
fs.readFile(filePath, function(err, data) {
if(err) {
handleError(err, res);
return;
} else {
res.end(data);
}
});
};

module.exports = fileRead;

Though, I am unable to understand the second part which says while importing you an specify the base folder. Why do you need to know the base folder?