为什么我的节点静态文件服务器丢弃请求?

时间:2012-06-13 22:09:52

标签: http node.js

我有一个标准的node.js静态文件服务器,我想用它来提供同一目录中的普通html,js,css和jpg文件(即典型的HTML5单页应用程序)。我希望节点服务器可以正确处理这个问题。我所看到的是不同的。

提供index.html文件,但后续请求被删除(即 - 它们从未进入服务器)。在我的chrome dev工具中,我看到如下内容:

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

但是,这些资源存在于服务器上,如果您直接输入其URL,则可以访问它们。对于这些请求,我永远不会调用app.js中的回调(我可以告诉它,因为永远不会为这些文件调用console.log

这是app.js文件:

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);

我在这里缺少什么?

我正在使用节点v0.6.15

1 个答案:

答案 0 :(得分:2)

最后,答案是我的cache.manifest文件不正确。客户端应用程序正在查找缓存中的资源,但不存在。当我纠正清单时,事情就开始起作用了。