Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot GET /" with Connect on Node.js

I'm trying to start serving some static web pages using connect like this:

var connect = require("connect"); var nowjs = require("now"); var io = require("socket.io");   var app = connect.createServer(   connect.static(__dirname + '/public') );  app.listen(8180); 

So I added a simple index.html at the /public directory on the same directory as the app.js file is, but when I try to view the page on my browser I get this response from node:

Cannot GET /

What I'm doing wrong and how I can correct it?

like image 851
Nathan Campos Avatar asked Mar 09 '12 00:03

Nathan Campos


People also ask

Can not get in NodeJS?

To fix the 'Error: Cannot GET /' message with Node. js and Express, we need to add the route handler for the GET / route. app. get("/", (req, res) => { res.

What is connect in node JS?

Connect is an extensible HTTP server framework for node using "plugins" known as middleware. var connect = require('connect'); var http = require('http'); var app = connect(); // gzip/deflate outgoing responses var compression = require('compression'); app.

How do I open a link in node JS?

To use Node. js to open default browser and navigate to a specific URL, we can use the open package. const open = require("open"); open("http://example.com"); open("http://example.com", { app: "firefox" }); to call open to open the URL we pass into it.


2 Answers

You'll see the message Cannot GET / if you don't specify which page it is that you're trying to get, in other words if your URL is something like http://localhost:8180. Make sure you enter a page name, e.g. http://localhost:8180/index.html.

like image 85
Stuart Hallows Avatar answered Nov 05 '22 06:11

Stuart Hallows


You may be here because you're reading the Apress PRO AngularJS book...

As is described in a comment to this question by KnarfaLingus:

[START QUOTE]

The connect module has been reorganized. do:

npm install connect  

and also

npm install serve-static 

Afterward your server.js can be written as:

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

[END QUOTE]

Although I do it, as the book suggests, in a more concise way like this:

var connect = require('connect'); var serveStatic = require('serve-static');  connect().use(     serveStatic("../angularjs") ).listen(5000); 
like image 40
Serj Sagan Avatar answered Nov 05 '22 07:11

Serj Sagan