1

URL: http://example.com:8080/js/file.js

var express = require('express'); app = express(); app.use(express.static('public')); app.listen(8080); 

Directory Structure

/ index.js (loaded node file) public (folder) ----js (folder) ----file.js (requested file) 

Error: Cannot GET /js/file.js

1
  • although you should provide full path your code is working on my machine, try app.use(express.logger('dev')); and see the output of console in which node.js is running the file Commented Nov 20, 2013 at 6:25

3 Answers 3

2

Provide the full path to the directory:

app.use(express.static(__dirname + '/public')); 
Sign up to request clarification or add additional context in comments.

Comments

0

It might be easier to set up something like what is described in this tutorial

http://www.mfranc.com/node-js/node-js-simple-web-server-with-express/

/* serves all the static files */ app.get(/^(.+)$/, function(req, res){ console.log('static file request : ' + req.params); res.sendfile( __dirname + req.params[0]); }); 

Comments

0

What version of express are you using? For me, using 3.4.0, the following didn't work:

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

but this did:

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

Not sure if its version specific, but usign the first syntax if was failing with the same Error: Cannot get XXX error

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.