简体   繁体   中英

expressjs Static Files not being served

I'm stuck on a frustrating problem.

My static files aren't being served by express js, and instead my global catch all get handler is being hit.

My directory structure:

node/apps/web/app.js
-------------/public/
--------------------/css/site.css
--------------------/js/
--------------------/img/

My development configuration

app.configure('development',function() {

console.log(__dirname);

// for development, serve static files via express
app.use(express.static(__dirname + '/public'));

app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});
//...later
app.get('*',route.r404);

The request:

http://192.168.1.101/css/site.css

Fails to trigger the proper css file.

EDIT TO ADD:

looks like it has something to do with the default catch all route. If I comment it out, it resolves properly...

If app.use(app.router) is above the app.use(express.static(...)) line then the routes will trump the static. Your app.get('*',...) line is part of the routes.

See Node.js / Express.js - How does app.router work? for more info.

 var express = require("D:/node/express/modules/express");
 var path=require("D:/node/express/modules/path");
 var app = new express();
 app.get('/',function(req,res){
     res.sendFile(path.join(__dirname,'/index.html'));
 });
 app.get('/about',function(req,res){
    res.sendFile(path.join(__dirname,'/about.html'));
 });
 app.listen(1000);

here var express and var path are modules path here you can use this simple code to serve static files.

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