1

I have a standard node.js static file server that I want to use to serve normal html, js, css, and jpg files in the same directory (ie- a typical HTML5 single page app). I would expect that the node server can handle this properly. What I see is different.

The index.html file is served, but then subsequent requests are dropped (ie- they never make it to the server). In my chrome dev tools, I see things like this:

GET http://projectcoho.cloudfoundry.com/css/coho.css  http://projectcoho.cloudfoundry.com/:7
GET http://projectcoho.cloudfoundry.com/sencha-touch/sencha-touch-debug.js  http://projectcoho.cloudfoundry.com/:8
GET http://projectcoho.cloudfoundry.com/coho-debug.js  http://projectcoho.cloudfoundry.com/:8

But, these resources exist on the server and you can reach them if you enter their URL directly. And for these requests, my callback in app.js is never invoked (I can tell this because console.log is never called for these files.

Here is the app.js file:

var path = ".";
var port = process.env.VCAP_APP_PORT || 3000;;

var file = new(static.Server) (path, {
  cache: 600
});

mime.define({
   'text/css': ['css'],
   'text/javascript': ['js'],
   'image/jpeg': ['jpg', 'jpeg']
});

http.createServer(function (request, response) {

    var uri = url.parse(request.url).pathname;
    var filename = libpath.join(path, uri);

    console.log("URI: " + request.url + " , filename: " + filename);

    libpath.exists(filename, function (exists) {
        console.log("Serving " + filename);
        if (!exists) {
            console.log("Not found");
            response.writeHead(404, {
                "Content-Type": "text/plain"
            });
            response.write("404 Not Found\n");
            response.end();
            return;
        }

        if (fs.statSync(filename).isDirectory()) {
            filename += '/index.html';
        }

        var type = mime.lookup(filename);
                file.serveFile(filename, 200, {'content-type' : type}, request, response);
    });
}).listen(port);

What am I missing here?

I am using node v0.6.15

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • Andrew -- what is the "port" value - ie is your server listening on the correct port? – Murray McDonald Jun 13 '12 at 22:14
  • Do filesystem permissions allow your server to open and read the file? What happens if the requested file is a directory but that directory does not contain the `index.html` file? – sarnold Jun 13 '12 at 22:14
  • @MurrayMcDonald When running on CloudFoundry, the port number is provided by the framework, but when running on localhost, the port is 3000. Behavior is the same in either case. – Andrew Eisenberg Jun 13 '12 at 22:17
  • @sarnold This is a case that i have not implemented yet. I'm pretty sure the server would barf, but I'm trying to fix the bigger problem first. – Andrew Eisenberg Jun 13 '12 at 22:19
  • and what is "rewriting" the URLS supplied to the browser to route the requests to the correct port? Is that "built in" to CloudFoundry itself? – Murray McDonald Jun 13 '12 at 22:20
  • I think showing the link to CloudFoundry is a red herring. This happens when I run on localhost as well. I included the pieces about cloudfoundry in case anyone wanted to navigate to the url and try it out themselves. – Andrew Eisenberg Jun 13 '12 at 22:23
  • @MurrayMcDonald to answer your question, yes. Cloud Foundry rewrites the urls to use the appropriate port. – Andrew Eisenberg Jun 13 '12 at 22:24
  • OK -- so when you are running locally and listening on port 3000 aand you serve out "index.html", do the URLS in that file route requests back to port 3000 ie http:://localhost:3000/some_file_name.hmtl or are the URLS just in the for http://localhost/some_file_name.html -- in which case the request is geting routed to port 80 by default? – Murray McDonald Jun 13 '12 at 22:28
  • Good question! I don't know. How would I find out? The css files are using link rel tags like this: and the js files use style tags. – Andrew Eisenberg Jun 13 '12 at 22:34
  • More importantly, if this is the case, then presumably all I need to do is to listen on both ports 3000 and 80? – Andrew Eisenberg Jun 13 '12 at 22:35
  • Not working. I notice that when I run on localhost. All the jpg, js, and css requests are going through port 3000. Listening on port 80 does nothing. – Andrew Eisenberg Jun 13 '12 at 22:37
  • What is standard node.js static file server? – Paul Verest May 02 '13 at 08:37
  • I don't understand your question. You should probably ask it in a new thread. – Andrew Eisenberg May 02 '13 at 16:46

1 Answers1

2

In the end, the answer was that my cache.manifest file was incorrect. The client application was looking for resources in a cache, but the didn't exist. When I corrected the manifest, things started working.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148