为什么我的节点静态文件服务器丢弃请求? [英] Why is my node static file server dropping requests?

查看:101
本文介绍了为什么我的节点静态文件服务器丢弃请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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.

这是app.js文件:

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

我在这里缺少什么?

我使用的是节点v0.6.15

I am using node v0.6.15

推荐答案

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

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.

这篇关于为什么我的节点静态文件服务器丢弃请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆