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

15,785

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 /.

Share:
15,785
evahriekes
Author by

evahriekes

Updated on June 26, 2022

Comments

  • evahriekes
    evahriekes almost 2 years

    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?

  • evahriekes
    evahriekes about 9 years
    I tried to add this : app.get('/', function(req,res) { res.sendfile('index.html'); } ); but I got the following error in the browser: Error: ENOENT, stat 'C:\Users\eva\Desktop\twitter application\server\index.html' at Error (native)
  • vanadium23
    vanadium23 about 9 years
    @evahriekes Yes, because your index.html is in client folder, try to move it in server folder.
  • evahriekes
    evahriekes about 9 years
    now the index.html is working but in static mode only...there is no dynamic code received from twitter i.e my application do not work as it should work @vanadium23
  • vanadium23
    vanadium23 about 9 years
    Okay, let's try following. Add app.use(express.static('../client')); and try to open http://localhost:8080/index.html
  • evahriekes
    evahriekes about 9 years
    now that works for me thank you very much @vanadium23