Error: Cannot GET /

8.5k views Asked by At

I am working in c9.io ide environment, I have written below code in server.js file

var http = require('http');
var path = require('path');

var async = require('async');
var socketio = require('socket.io');
var express = require('express');

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

var router = express();
var server = http.createServer(router);

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("Server listening at", addr.address + ":" + addr.port);
});

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

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
  res.render('index.html');
});

app.get('/about', function (req, res) {
  res.send('about');
});

After running node server.js in terminal the message given as

Your code is running at https://nodejs2-mujaffar.c9.io.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!

Server listening at 0.0.0.0:8080

But after accessing https://nodejs2-mujaffar.c9.io/ url -- It is not rendering view only displaying message Error: Cannot GET /

What I am doing wrong?

Please help.

Folder structure of app

1

There are 1 answers

0
Varedis On BEST ANSWER

You seem to have created two instances of express which may be your problem.

Try changing:

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

var router = express();
var server = http.createServer(router);

to:

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

var server = http.createServer(app);

At the minute, your express app variable is not bound to your http server. You have instead bounded an unused instance called router. But then you have registered your routes to the app variable.