Alex Alex - 1 year ago 90
Node.js Question

NodeJS won't serve static files, even when using express.static

I have something along the lines of the following:

var request = require('request'),
express = require('express');

var app = express.createServer();

var port = process.env.PORT || 8080;

app.set("view options", { layout: false, pretty: true });
app.use("/public", express.static(__dirname + '/public'));


// Routes
app.get('/', function(req, resp){
resp.render('index.jade', {pageTitle: 'Some title'});

Yet, when I visit /public/myfile.css for example, I still get:

Cannot GET /public/myfile.css
My index.jade templates cannot seem to call the files either

Why is this?

Answer Source

I don't think supplying the path like that is supported:

app.use("/public", express.static(__dirname + '/public'));

Try this, and look for your public files in the root:

app.use(express.static(__dirname + '/public'));

So /public/myfile.css becomes /myfile.css.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download