Getting 404 for stylesheets and js on sample express-jade project Getting 404 for stylesheets and js on sample express-jade project express express

Getting 404 for stylesheets and js on sample express-jade project


In your app.js, mount the static files earlier, like so:

app.configure(function(){  app.set('port', process.env.PORT || 3000);  app.set('views', __dirname + '/views');  app.set('view engine', 'jade');  app.use(express.static(path.join(__dirname, '/public')));  app.use(express.favicon());  app.use(express.logger('dev'));  app.use(express.bodyParser());  app.use(express.methodOverride());  app.use(app.router);});

Edit: Note the in /public VS public


Like markt mentioned. Your html should not be referencing "public".

<link href="../../styles/site.css" rel="stylesheet" />

vs.

<link href="../../public/styles/site.css" rel="stylesheet" />


In my case it was necessary to specify sub directories, as well as the main "public" static directory, eg:

app.use(express.static(path.join(__dirname, 'public')));app.use('/public/uploads', express.static(path.join(__dirname, '/public/uploads')));

Also after playing around I found that it made no difference whether the "public" static directory was declared as "/public" or "public" (with or without leading slash).

However it did make a difference if I missed the leading slash from the sub directories, ie this gave me 404:

app.use('public/uploads', express.static(path.join(__dirname, 'public/uploads')));

but this worked ok:

app.use('/public/uploads', express.static(path.join(__dirname, '/public/uploads')));

And of course the old chestnut of making sure permissions are correctly set on the directory!