1
votes

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

1
Andrew -- what is the "port" value - ie is your server listening on the correct port?Murray McDonald
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
@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
@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
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

1 Answers

2
votes

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.