2012-05-02 73 views
2

我已經建立了一個基本的節點/快遞服務器,它爲公共靜態javascript和css文件提供了很好的服務,但在嘗試提供圖像時返回404錯誤。ExpressJS /節點: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); 

回答

2

如果它工作正常本地,也許這是一個區分大小寫的問題,請您的文件有資金等?

+0

不,沒有資本化。 –

+0

Alrighty,我通過將我的圖像文件夾從「/ public/images」重命名爲「/ public/image」來解決問題。我仍然不知道爲什麼命名會導致問題,但我很高興這就是所需要的。 我會回答這個問題,並在達到時間限制時關閉它。 –

+0

我認爲它與我的Linode機器有關。 404來自nginx而不是節點。感謝TJ的回覆;我有更多的研究要做。 –

0

Alrighty,我通過將我的images文件夾從「/ public/images」重命名爲/ public/image來解決了這個問題。我不知道爲什麼命名會導致問題,但我很高興這就是所需要的。

0

我有這個確切的問題。上傳到/static/uploads的所有用戶生成的圖像均未通過快遞呈現。奇怪的是,static/images,static/js,static/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; 
    } 
} 
1

我有這個問題,但它結束了,我不得不在後.JPG擴展帽,並呼籲在HTML .JPG。 Windows對文件類型不區分大小寫,CentOS是...