Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot GET / Nodejs Error

I'm using the tutorial found here: http://addyosmani.github.io/backbone-fundamentals/#create-a-simple-web-server and added the following code.

// Module dependencies.
var application_root = __dirname,
express = require( 'express' ), //Web framework
path = require( 'path' ), //Utilities for dealing with file paths
mongoose = require( 'mongoose' ); //MongoDB integration

//Create server
var app = express();

// Configure server
app.configure( function() {
//parses request body and populates request.body
app.use( express.bodyParser() );

//checks request.body for HTTP method overrides
app.use( express.methodOverride() );

//perform route lookup based on url and HTTP method
app.use( app.router );

//Where to serve static content
app.use( express.static( path.join( application_root, 'site') ) );

//Show all errors in development
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
});

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

After starting the server with node server.js I'm getting an error stating Cannot GET / when I access localhost:5000 and I just wondered if anyone knew much about this error as Express and Node are new to me?

like image 733
tommyd456 Avatar asked Jan 23 '14 19:01

tommyd456


People also ask

What is Node JS error?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


2 Answers

I think you're missing your routes, you need to define at least one route for example '/' to index.

e.g.

app.get('/', function (req, res) {
  res.render('index', {});
});
like image 176
Jon Miles Avatar answered Oct 02 '22 15:10

Jon Miles


Have you checked your folder structure? It seems to me like Express can't find your root directory, which should be a a folder named "site" right under your default directory. Here is how it should look like, according to the tutorial:

node_modules/
  .bin/
  express/
  mongoose/
  path/
site/
  css/
  img/
  js/
  index.html
package.json

For example on my machine, I started getting the same error as you when I renamed my "site" folder as something else. So I would suggest you check that you have the index.html page inside a "site" folder that sits on the same path as your server.js file.

Hope that helps!

like image 34
leonardocsouza Avatar answered Oct 02 '22 15:10

leonardocsouza