ExpressJS / Node:404张图片

时间:2012-05-02 14:43:51

标签: image node.js express http-status-code-404 linode

我已经设置了一个基本的节点/快速服务器,它可以很好地提供公共静态javascript和css文件,但在尝试提供图像时会返回404错误。

最奇怪的是,在本地运行时一切正常。在我的远程服务器(linode)上运行时,图像问题就会出现。

这真让我挠头......可能是什么问题?

这是服务器:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
  app.use(express.static(__dirname + '/public'));
  app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});

// Globals

app.set('view options', {
  sitename: 'Site Name', 
  myname: 'My Name'
});

// Routes

app.get('/', routes.index);
app.get('/*', routes.fourohfour);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

3 个答案:

答案 0 :(得分:2)

如果它在本地工作正常,可能是一个区分大小写的问题,你的文件是否有大写字母等?

答案 1 :(得分:0)

好吧,我通过将我的images文件夹从“/ public / images”重命名为/ public / image来解决这个问题。我不知道为什么命名会引起问题,但我很高兴这就是所有需要的。

答案 2 :(得分:0)

我有这个问题。上传到/static/uploads的所有用户生成的图像均未通过快递呈现。奇怪的是static/imagesstatic/jsstatic/css中的所有内容都很好。我确保它不是权限问题,但仍然得到404.最后我配置NGINX来渲染我的静态文件的所有(无论如何它可能更快)并且它有效!

我仍然很想知道为什么Express不会撕毁我的照片。

如果有人遇到这个问题,这是我的NGINX conf:

server {

    # listen for connections on all hostname/IP and at TCP port 80
    listen *:80;

    # name-based virtual hosting
    server_name staging.mysite.com;

    # error and access outout
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location / {
            # redefine and add some request header lines which will be transferred to the proxied server
            proxy_set_header                X-Real-IP $remote_addr;
            proxy_set_header                X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header                 Host $http_host;
            proxy_set_header                X-NginX-Proxy true;

            #set the location of the web root
            root /var/www/mysite.com;

            # set the address of the node proxied server. port should be the port you set in express
            proxy_pass                      http://127.0.0.1:9001;

            # forbid all proxy_redirect directives at this level
            proxy_redirect                  off;
    }

    # do a case insensitive regular expression match for any files ending in the list of extentions
    location ~* ^.+\.(html|htm|png|jpeg|jpg|gif|pdf|ico|css|js|txt|rtf|flv|swf)$ {

            # location of the web root for all static files
            root /var/www/mysite.com/static;

            # clear all access_log directives for the current level
            #access_log off;

            # set the Expires header to 31 December 2037 23:59:59 GMT, and the Cache-Control max-age to 10 years
            #expires max;
    }
}