简体   繁体   中英

express.static() 404 errors when attempting to serve /css and /js files

I am currently working on a project using the Express framework. I have a directory "/static", from which I'd like to serve static js and css files.

The project directory structure is as follows:

/
  visits.js
  /static
    /css
      style.css
    /js
      jquery.flot.js
  /views

In the app configuration, I am setting up the static file server with the following:

app.configure(function(){
    app.use(express.logger());
    app.use(express.static(__dirname + '/static'));
    app.set('views', __dirname + '/views');
    app.set('view options', { layout : true });
    app.set('view engine', 'jade');
});

When I run the app on my local machine, everything works great. My network console shows:

GET http://myserver.com/ [HTTP/1.1 200 OK 13ms]
GET http://myserver.com/css/style.css [HTTP/1.1 200 OK 3ms]
GET http://myserver.com/js/jquery.flot.js [HTTP/1.1 200 OK 4ms] 

However, when I push the same application up to my test server, the static files are not found (404 errors):

GET http://myserver.com/visits/ [HTTP/1.1 200 OK 195ms]
GET http://myserver.com/css/style.css [HTTP/1.1 404 Not Found 95ms]
GET http://myserver.com/js/jquery.flot.js [HTTP/1.1 404 Not Found 93ms]

The only difference that I can think of is on my local machine, I simply access the application at:

127.0.0.1:4000/

But on the test server, I have the app sitting behind an nginx server. I use a proxy_pass that forwards requests to:

myserver.com/visits/

I'm wondering if the forwarding is affecting the path that the static file server is using to look up the css and js files?

Any ideas / help would be greatly appreciated.

Thanks!

After further investigation, this was an issue with my use of proxy_pass in the nginx configuration. The initial GET request was being properly forwarded to my Express app, but the subsequent calls for css or js files was not being properly routed. This is not an issue with the Express framework. Thanks for looking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM