Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot GET /test.html when running Node.js server

I am beginning to read Pro AngularJS. In the section to setup the development environment it has me create a angularjs directory and put a test.html file into it. Outside of that folder I installed 'connect' and 'serve-static' for Node.js. I also created a server.js file. The contents are seen below:

var connect = require('connect');
var app = connect().use(connect.static('/angularjs'));
app.listen(5000);

When visiting the following URL http://localhost:5000/test.html all I see is the text "Cannot GET /test.html".

I have looked at this and this question here on SO. None of the solutions were helpful for me.

like image 869
nbonbon Avatar asked Aug 22 '14 23:08

nbonbon


People also ask

Can NodeJS be used with HTML?

Run nodemon to start the server. Whenever the server is running, and accessing the route http://localhost:3000/ , it will output the plain text hello world! . We can use the same server to render HTML elements as the server response instead of sending plain text. Here is a list of some HTML elements.


2 Answers

The book didn't really do a good job of describing where to put server.js that I remember. So I had it one folder above the angularjs folder. Therefore, the '..' needed to be removed. I made the mistake of leaving in the '/' which caused my problems. Just as I figured ... a stupid small mistake (hardest to find).

like image 125
nbonbon Avatar answered Oct 13 '22 03:10

nbonbon


First, make sure your ../angularjs folder contains test.html file?

Then, you can try to resolve path

var connect = require('connect');
var path = require('path');
var app = connect().use(connect.static(path.resolve(__dirname, '..', 'angularjs')));
app.listen(5000);
like image 40
Hung Nguyen Avatar answered Oct 13 '22 02:10

Hung Nguyen