NodeJS w/Express Error: Cannot GET / NodeJS w/Express Error: Cannot GET / express express

NodeJS w/Express Error: Cannot GET /


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.


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.


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) => { ... }    )