Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“Cannot GET /” when trying to connect to localhost:8080 in node.js [closed]

I am working on backbone.js application with node.js as backend server. the application is based on twitter API. When requesting to localhost:8080 I got the error "Cannot GET /" in the browser and I do not know what is the matter. My code is as follows:

server.js

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

var Twit = require('twit')

var client = null;


function connectToTwitter(){
   client = new Twit({
      consumer_key:         'xxx'
    , consumer_secret:      'xxx'
    , access_token:         'xxx'
    , access_token_secret:  'xxx'
  });
}

//get the app to connect to twitter.
connectToTwitter();

var allowCrossDomain = function(req, response, next) {
response.header('Access-Control-Allow-Origin', "http://localhost");
response.header('Access-Control-Allow-Methods', 'OPTIONS, GET,PUT,POST,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type');

if ('OPTIONS' == req.method) {
  response.send(200);
}
else {
  next();
}
};

app.configure(function() {
    app.use(allowCrossDomain);
  //Parses the JSON object given in the body request
    app.use(express.bodyParser());
});

//Start server
var port = 8080;
app.listen( port, function() {
  console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});

Directory structure in general:

C:\Users\eva\Desktop\twitter application\server\server.js 
                                        \client\index.html 
                                                \js

Can someone tell me why this error occur?

like image 914
evahriekes Avatar asked May 01 '15 12:05

evahriekes


People also ask

Why local host 8080 is not working?

You need to access your app with http in the URL not https when developing locally. You may have your web browser set to automatically try to upgrade the connection from http to https. If you, disable this setting.

Why my localhost is not working?

If you're unable to access the web server via localhost, there's a chance that your firewall may be blocking the connection. You can often resolve this problem by modifying your firewall settings to allow incoming connections for the port that MAMP is trying to access.


1 Answers

You need to implement what server must response on / requests. For example:

app.get('/', function(req, res, next) {
    res.send("Hello world");
});

Without it, express doesn't know how to handle /.

like image 143
vanadium23 Avatar answered Sep 30 '22 05:09

vanadium23