-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 6: index.js cannot serve static files this way ! #3
Comments
Thank you! It was obvious something was wrong with the code but I couldn't figure it out. I replaced the old code with:
which does not work. I was stumped. Can you or someone explain why we send two arguments to staticFile when exports.staticFile only asks for one? |
// from ./responseGenerator.js
// (...)
exports.staticFile = function (staticPath) {
\*************** LOOK HERE !! **********************\
return function(data, response) {
var readStream;
// Fix so routes to /home and /home.html both work.
data = data.replace(/^(\/home)(.html)?$/i,'$1.html');
data = '.' + staticPath + data;
fs.stat(data, function (error, stats) {
if (error || stats.isDirectory()) {
return exports.send404(response);
}
readStream = fs.createReadStream(data);
return readStream.pipe(response);
});
}
} When you require the module, then call // from index.js
var responder = require('./responseGenerator');
var staticFile = responder.staticFile('/public'); you get a |
|
You should replace the last lines of
index.js
with
The program as is will continue to respond with
'static file maybe'
to every request not intercepted by the preceding routing tests.The text was updated successfully, but these errors were encountered: