405859 (6) [Avatar] Offline
#1
Hello,
In listing 3.10, I couldn't get it to work as written -- I had to move these lines:

var publicPath = path.resolve(__dirname,"public");
app.use(express.static(publicPath));

app.use(function(request,response){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.end("Looks like you didn't find a static file in " + publicPath + "\n");
});

after all of the app.get() calls. Otherwise, it would always complain that it couldn't find a file.

Is there something that I'm missing, or is this a typo in the book?

Incidentally, I think the book is great (as far as I've read, which is listing 3.10 smilie), and the tone of the book (I heard a few grumblings in other parts of this forum) is perfectly fine, IMHO.

Michael
Evan Hahn (70) [Avatar] Offline
#2
Hm, everything looks alright to me.

I made a tiny app like this:

var express = require("express");
var path = require("path");

var app = express();

var publicPath = path.resolve(__dirname,"public");
app.use(express.static(publicPath));

app.use(function(request,response){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.end("Looks like you didn't find a static file in " + publicPath + "\n");
});

app.listen(3000, function () {
  console.log("App started on port 3000");
});


And if there's a file called my-file.txt in a folder called public, I can get to it at localhost:3000/my-file.txt.

Does your file structure look something like this?

.
??? app.js
??? package.json
??? node_modules/
??? public/
    ??? my-file.txt
405859 (6) [Avatar] Offline
#3
Thanks for getting back to me ... my file structure looks like this:

  
app.js		app_post.js	node_modules/	package.json	public/

with testing.txt and testing2.txt in public.

My code (essentially listing 3.10) looks like this:
var express = require("express");
var path = require("path");
var http = require("http");

var app = express();

var publicPath = path.resolve(__dirname,"public");

app.use(express.static(publicPath));

app.use(function(request,response){
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.end("Looks like you didn't find a static file in " + publicPath + "\n");
});

app.use(function(request,response){
        response.statusCode = 404;
        response.end("404 :-(");
});


app.get("/", function(request,response){
        response.end("Welcome to my homepage");
});

app.get("/about", function(request,response){
        response.end("Welcome to the about page!");
});


app.get("/weather", function(request,response){
        response.end("The current weather is fabulous.");
});

http.createServer(app).listen(3000);


When I try
 curl localhost:3000/about 
I get
Looks like you didn't find a static file in /Users/me/Dropbox/Public/Stuff/IoT-f16/Doodles/expendable_node_js/public

but when I try
 curl localhost:3000/testing.txt 
it works as expected.
Evan Hahn (70) [Avatar] Offline
#4
Ah, I see what's going wrong. Some of your middleware/routes are in the wrong order.

Here's at your code should look like. I've added some comments that aim to help explain what's going on.

// This is the first middleware in the stack.
// If it finds a static file, it will not continue on.
app.use(express.static(publicPath));

// If the request is for /, this function will be called.
// If it's NOT for /, this function won't be called and Express will try the next middleware.
app.get("/", function(request,response){
        response.end("Welcome to my homepage");
});

// If the request is for /about, this function will be called.
// If it's NOT for /about, this function won't be called and Express will try the next middleware.
app.get("/about", function(request,response){
        response.end("Welcome to the about page!");
});

// Same for /weather—only call this function if it's for /weather, continuing otherwise.
app.get("/weather", function(request,response){
        response.end("The current weather is fabulous.");
});

// If NONE of the other middlewares are called, call this one—send a 404.
app.use(function(request,response){
        response.statusCode = 404;
        response.end("404 :-(");
});


Hope that helps!
405859 (6) [Avatar] Offline
#5
That clears it up completely. I'd thought I'd reorganize them (thinking that serving a file and not finding a file seemed related), but it's calling the middleware functions in the order in which the app.use() calls appear. Order does matter, I need to remember that!

Thanks for the clarification, and for the great book.
Evan Hahn (70) [Avatar] Offline
#6
No problem! Let me know if you run into any other issues.