0

I'm completely new to node, and i was trying to get this example to work:

[/path/to/node/server.js]

var connect = require('connect'); 
var serveStatic = require('serve-static'); 
var app = connect(); 
app.use(serveStatic('../angularjs'), {default: "test.html"}); 
app.listen(5000);

I have the file test.html inside the angularjs folder: [/path/to/node/angularjs/test.html]

But when i request (localhost:5000/test.html) i'm getting in the navigator:

Cannot GET /test.html.

Any ideas?

1
  • You may want to be using Express 4.x which offers great functionality in node, but uses a different initialization sequence than this and doesn't need the connect module. Commented Oct 21, 2014 at 22:49

1 Answer 1

2

You'll want to supply server-static with an absolute path to angularjs to ensure it's searching in the expected path.

You can use the script's __dirname as the base for it:

...serveStatic(__dirname + '/angularjs')...

Or with path.join():

var path = require('path');
// ...

...serverStatic(path.join(__dirname, 'angularjs'))...

This is because the file system module, that serve-static is using, resolves relative paths from the current working directory (process.cwd()).

So, whether ../angularjs resolves to /path/to/node/angularjs depends on which directory server.js is started from.

Currently, it could work from /path/to/node/angularjs/:

$ cd /path/to/node/angularjs
$ node ../server.js

# resolves: '/path/to/node/angularjs' + '../angularjs'
# to:       '/path/to/node/angularjs'

Or, by changing the ../ (parent directory) to ./ (current directory), it could be started from /path/to/node instead:

...serveStatic('./angularjs')...
$ cd /path/to/node
$ node server.js

# resolves: '/path/to/node' + './angularjs'
# to:       '/path/to/node/angularjs'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.