82

This is what i have, the filename "default.htm" actually exists and loads when doing a readFile with NodeJS.

var express = require('express');
var app = express();

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

app.listen(process.env.PORT);

The Error (in browser):

Cannot GET /
sia
  • 2,185
  • 4
  • 20
  • 17

18 Answers18

53

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.

Ilija
  • 4,105
  • 4
  • 32
  • 46
Sdedelbrock
  • 5,192
  • 1
  • 18
  • 13
24

I've noticed that I forgot the "slash" in the beginning of the Route as below and I was getting same error :

Wrong :

app.get('api/courses',  (req, res) => { ... }
)

Correct :

  app.get('/api/courses',  (req, res) => { ... }
    )
Bulent Balci
  • 644
  • 5
  • 11
18

You need to define a root route.

app.get('/', function(req, res) {
  // do something here.
});

Oh and you cannot specify a file within the express.static. It needs to be a directory. The app.get('/'.... will be responsible to render that file accordingly. You can use express' render method, but your going to have to add some configuration options that will tell express where your views are, traditionally within the app/views/ folder.

Daniel
  • 1,692
  • 2
  • 13
  • 19
10

I had the same problem, so here's what I came up with. This is what my folder structure looked like when I ran node server.js

app/
  index.html
  server.js

After printing out the __dirname path, I realized that the __dirname path was where my server was running (app/).

So, the answer to your question is this:

If your server.js file is in the same folder as the files you are trying to render, then

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

should actually be

app.use(express.static(__dirname));

The only time you would want to use the original syntax that you had would be if you had a folder tree like so:

app/
  index.html
server.js

where index.html is in the app/ directory, whereas server.js is in the root directory (i.e. the same level as the app/ directory).

Overall, your code could look like:

var express = require('express');

var app = express();

app.use(express.static(__dirname));

app.listen(process.env.PORT);
CopyLeft
  • 321
  • 4
  • 6
6

I found myself on this page as I was also receiving the Cannot GET/ message. My circumstances differed as I was using express.static() to target a folder, as has been offered in previous answers, and not a file as the OP was.

What I discovered after some digging through Express' docs is that express.static() defines its index file as index.html, whereas my file was named index.htm.

To tie this to the OP's question, there are two options:

1: Use the code suggested in other answers

app.use(express.static(__dirname));

and then rename default.htm file to index.html

or

2: Add the index property when calling express.static() to direct it to the desired index file:

app.use(express.static(__dirname, { index: 'default.htm' }));
PTD
  • 1,028
  • 1
  • 16
  • 23
  • Thanks for this it fixed my problem. For some reason Index.html was fine in windows but I had to specify it in linux. – Sam Marion Jul 06 '17 at 03:34
3

Where is your get method for "/"?

Also you cant serve static html directly in Express.First you need to configure it.

app.configure(function(){
  app.set('port', process.env.PORT || 3000);  
  app.set("view options", {layout: false});  //This one does the trick for rendering static html
  app.engine('html', require('ejs').renderFile); 
  app.use(app.router);

});

Now add your get method.

app.get('/', function(req, res) {
  res.render('default.htm');
});
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
3

You need to add a return to the index.html file.

app.use(express.static(path.join(__dirname, 'build')));

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

xplorer1
  • 1,254
  • 1
  • 10
  • 13
3

Provide full path for example I am running my app on https://ugoods.in/nodeapp

app.get('/nodeapp', (req, res) => {
  res.send('Hello World!')
})

If you will run directly on http://ugoods.in or http://localhost

app.get('/', (req, res) => {
  res.send('Hello World!')
})
Md Rehan
  • 309
  • 2
  • 8
  • thanks man. was tired from this. for future readers, I was hosting django as well as node app on the same server, so node was configured to run on different port and url, for example, my django project was running on "/" and node on "/node" so I had to add the full path in express post("/node/my-api") – Fusion Developers Nov 27 '22 at 19:11
2

I had the same issue. Solved it by small changes like below.

var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));

Got help from here (ExpressJS Documentation - Serving static files).

2

In my case, the static content was already being served:

app.use('/*', express.static(path.join(__dirname, '../pub/index.html')));

...and everything in the app seemed to rely on that in some way. (path dep is require('path'))

So, a) yes, it can be a file; and b) you can make a redirect!

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

Now anyone hitting / gets /index.html which is served statically from ../pub/index.html.

Hope this helps someone else.

Chaim Eliyah
  • 2,743
  • 4
  • 24
  • 37
1

I was facing the same problem as mentioned in the question. The following steps solved my problem.

I upgraded the nodejs package link with following steps

  1. Clear NPM's cache:

    npm cache clean -f
    
  2. Install a little helper called 'n'

    npm install -g n  
    

Then I went to node.js website, downloaded the latest node js package, installed it, and my problem was solved.

user812786
  • 4,302
  • 5
  • 38
  • 50
Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
1
var path = require('path');

Change app.use(express.static(__dirname + '/default.htm')); to app.use(express.static(path.join(__dirname + '/default.htm')));.

Also, make sure you point it to the right path of you default.html.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Officialzessu
  • 897
  • 7
  • 6
1

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

Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66
1

"Cannot " happens when a request is sent to non-defined target. For example:

app.get('/',()=>{});

This would handle requests sent to localhost/ but not localhost/path.

Please, make soure there is a routing defined for the URL you are trying to access. Actual response you might get:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /path</pre>
</body>
</html>
1

I was also getting the same error as "Cannot GET /" in my MERN application in the network tab. I added these lines of code in my index.js(in backend) file after referring to this article https://coursework.vschool.io/deploying-mern-app-to-heroku/ :

Right before my app.listen():

app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});

and also changed a line in

if(process.env.NODE_ENV === 'production')

from this:

app.use(express.static('./client/build'));

to this:

app.use(express.static(path.join(__dirname, "client", "build")))

So, check this too, if making these changes work for you.

Suryakant
  • 11
  • 1
0

I had this exact issue, my error source might differ to what your source of error was though.

However, as for me, it was because I was running the app instance on CPanel inside subfolder as:

app.get("/", (request, response) => {
       response.status(200).send("Hello there");
})

Results:

Can not GET /

So, I realized I was setting wrong parameter, and what I did was:

app.get("/foldername/", (request, response) => {
       response.status(200).send("Hello there, it works!");
 })

Results:

Hello there, it works!
Amin Matola
  • 137
  • 2
  • 7
-1

Another solution is to check whether your .static() parameter refers to the correct folder of your other website files (.html, .css, ..). this folder should be in the same directory of the server.js file. don't know why though, I tried to put it in a different directory and pass ./website for example but it didn't work. :/

  • 1
    is this answer or you're trying to comment your opinion about question? can you clarify? – Lakshman Kambam Apr 18 '21 at 18:00
  • It's an answer, I had a similar question and other answers here didn't solve my problem. that's why I added this answer in case someone needed it in the future. :D I edited my answer. – Muhammad Ihab Apr 18 '21 at 20:47
-2

Instead of using "app.use", try to use "app.get". It works on my machine.

  • 1
    Your answer could be improved by providing an example of the solution and how it helps the OP. – Tyler2P May 03 '22 at 18:04