Configuring the application directory Node.js

advertisements

I'm completely new to using Node.js and even utilizing the command line, so this question may be extremely elementary, but I am unable to find a solution.

I am trying to set up an app directory using Node.js and NPM. For some reason, whenever I try to use the port:5000 I get a "Cannot GET/" error. My question is, why is my setup for my app directory not working?

I have installed connect and serve-static, and yet it will not retrieve files and listen on port 5000. I have created a server.js file in my user, kstach1. Here is the code I have within that file:

var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect(); 

app.use(serveStatic('../angularjs')); 

app.listen(5000);

So, I don't quite understand why this won't reference my folder of angularjs, where I want to store my app. I have tested it by adding a file within the folder called test.html, and entered localhost:5000/test.html, and still get the "Cannot GET/test.html" error.

I know that Node is working correctly because I can enter scripts into the command line and they give the correct output. I do this as a user (kstach1).

The only thing I can think of that I may be doing wrong, is where my files are located. I have the angularjs folder located in the root user folder on my Mac (kstach1), as well as the server.js file. Is this incorrect? If this is not the issue, is it because of where Node is installed (usr/local/bin/node)? My research to this point has led me to think that my problem could also be that I need to add the installation directory to my path. However, I don't want to mess with this unless I know that is the case.

Any help would be greatly appreciated.


I did a little research on the serve-static package and copied the code you provided.

My project folder is located at "C:\teststatic" and the folder with the static files is: "C:\angularjs", also using "text.html" that is located in the 'angularjs' folder.

When running the code you provided and going to localhost:5000 it indeed returns "Cannot GET/". This is most likely because there is no "/" file declared. Going to localhost:5000/test.html works for me, so you could try setting a "/" like this:

app.use(serveStatic('../angularjs', {'index': ['test.html', 'index.html']}));

And see if that works for you. If not, you should double check directory names / locations.

EDIT: From reading the comment you posted: try this instead:

app.use(serveStatic('angularjs'));