"Cannot GET /" with Connect on Node.js
Asked Answered
H

13

66

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 am I doing wrong and how can I correct it?

Haldan answered 9/3, 2012 at 0:52 Comment(0)
E
14

This code should work:

var connect = require("connect");

var app = connect.createServer().use(connect.static(__dirname + '/public'));

app.listen(8180);

Also in connect 2.0 .createServer() method deprecated. Use connect() instead.

var connect = require("connect");

var app = connect().use(connect.static(__dirname + '/public'));

app.listen(8180);
Eelgrass answered 9/3, 2012 at 1:41 Comment(2)
Ahh, thanks very much. All these old node.js articles makes me really confused with the deprecated things.Haldan
I have tried both of these and they both say that undefined is not a function. In the first example this tells me that .createServer() is not a function and in the second example __dirname is invalid.Gonsalez
S
38

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.

Subjugate answered 29/1, 2015 at 3:43 Comment(4)
yep... It just happened to me.Gallantry
Also same behaviour if you have the wrong path, for example to a static fileWhoosh
I think this solution is what will mostly work for those using express.jsWinfordwinfred
Simple but very useful answer. It fixed my simple "Cannot GET /" issueVerisimilitude
Q
26

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);
Quest answered 10/7, 2014 at 0:56 Comment(1)
Thanks for that also, helped me solve mine. Incidentally I'm going through the ProAngularJS book trying to get it set up on my MacBookPro and I ended up structuring my directory as "Apress/angularjs", where server.js resides in the "Apress" directory; in that case the path in the server.js code would be serveStatic("angularjs"). Once I did that the simple test in Chapter 1 of the Apress book succeeded and I'm now successfully serving up the test page. Thanks again @Serj for your post.Soak
E
14

This code should work:

var connect = require("connect");

var app = connect.createServer().use(connect.static(__dirname + '/public'));

app.listen(8180);

Also in connect 2.0 .createServer() method deprecated. Use connect() instead.

var connect = require("connect");

var app = connect().use(connect.static(__dirname + '/public'));

app.listen(8180);
Eelgrass answered 9/3, 2012 at 1:41 Comment(2)
Ahh, thanks very much. All these old node.js articles makes me really confused with the deprecated things.Haldan
I have tried both of these and they both say that undefined is not a function. In the first example this tells me that .createServer() is not a function and in the second example __dirname is invalid.Gonsalez
C
3

You might be needed to restart the process if app.get not working. Press ctl+c and then restart node app.

Crooks answered 1/3, 2019 at 11:7 Comment(0)
A
2

Had the same issue. It was resolved as described above.

In my index.js

var port = 1338,
express = require('express'),
app = express().use(express.static(__dirname + '/')),
http = require('http').Server(app),
io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
    console.log('a user connected');
});

http.listen(port, function(){
    console.log("Node server listening on port " + port);
});

and in my index.html

<!doctype html>
<html>
    <head>
        <title>
            My page
        </title>
    </head>
    <body>
        <script src = "lib/socket.io.js"></script>
        <script src = "lib/three.js"></script>
        <script>
            var socket = io();
        </script>
    </body>
</html>

the three.js was just in there for path testing. This will set all child files to start at the root directory of your app. Also socket.io.js can be called automatically using <script src = "/socket.io/socket.io.js"> through some dark magic (since there is physically a node_modules and lib directory in between) .

Ainsley answered 5/1, 2015 at 22:8 Comment(0)
C
2

The solution to "Cannot Get /" can usually be determined if you do an "ng build" in the command line. You will find most often that one of your "imports" does not have the correct path.

Chavannes answered 6/7, 2019 at 18:31 Comment(0)
C
1
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect(); 
app.use(serveStatic('../angularjs'),  {default: 'angular.min.js'}); app.listen(3000); 
Copperhead answered 4/7, 2014 at 10:56 Comment(3)
Create an angularjs directory. Make index.html file inside it.Copperhead
While this code block may answer the question, it would be best if you could provide a little explanation for why it does so.Halting
I am using serveStatic module, which does the job.Copperhead
D
1

You may also want to try st, a node module for serving static files. Setup is trivial.

npm install connect

npm install st

And here's how my server-dev.js file looks like:

var connect = require('connect');
var http = require('http');
var st = require('st');

var app = connect()
    .use(st('app/dev'));

http.createServer(app).listen(8000);

or (with cache disabled):

var connect = require('connect');
var http = require('http');
var st = require('st');

var app = connect();

var mount = st({
  path: 'app/dev',
  cache: false
});

http.createServer(function (req, res) {
  if (mount(req, res)) return;
}).listen(8000);

app.use(mount);
Drying answered 21/9, 2014 at 7:59 Comment(0)
T
0

The easiest way to serve static files is to use "harp". It can be found here. You can serve up your files from the location you want via node is:

var harp = require("harp")
harp.server(projectPath [,args] [,callback])

Hope this helps.

Terle answered 21/12, 2014 at 19:43 Comment(0)
Q
0
  1. open cmd and go to the path of your project.(make sure that you are in "clientApp")
  2. then build your project with "ng build" command. if there is an error the console will give you the error. build error

now you know what your error is.

Queue answered 9/6, 2022 at 11:36 Comment(0)
S
0

The Straight Forward Answer, include the code in your Node/Server index.js file

const path = require("path"); // include this in the file

router.get("/*", function(req, res) {
  res.sendFile(path.join(__dirname, "../public", "index.html"));
});

And if your are using navigate() in your reactApp use the above code at the end of the file.

Sapless answered 12/8, 2022 at 12:42 Comment(0)
B
0

That was a silly mistake , I commented out the line that app.js uses for referencing routes.

This fix was to remove using express() directly.

enter image description here

And inside app.js you can define your customize routes

enter image description here

Bus answered 20/8, 2023 at 18:56 Comment(0)
P
-1

You typically want to render templates like this:

app.get('/', function(req, res){
  res.render('index.ejs');
});

However you can also deliver static content - to do so use:

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

Now everything in the /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in the public folder if will be available by visiting /default.htm

Take a look through the express API and Connect Static middleware docs for more info.

Penicillin answered 29/11, 2013 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.