Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js server has no response

I'm using NODE.js and Express (express-generator) to create a website. I had this working just fine yesterday afternoon, but I guess I changed something and it doesn't work now. My firewall is turned off.

I get the following in my console.

C:\website>node bin/www
Listening on 8080
Get / - - ms - -
Get / - - ms - -
Get / - - ms - -

Each of those Get / - - ms - - happens each time I try to go to 127.0.0.1:8080

Here is my bin/www file:

#!/usr/bin/env node
var debug = require('debug')('test');
var app = require('../app');

app.set('port', process.env.PORT || 8080);

var server = app.listen(app.get('port'), function() {

  console.log("Listening on " + server.address().port);

  debug('Express server listening on port ' + server.address().port);
});

And my app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler

// production error handler
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    console.log(err);
});
    
module.exports = app;
like image 473
MaylorTaylor Avatar asked Sep 10 '14 15:09

MaylorTaylor


1 Answers

This behavior might be the result of incorrectly defined routes. Your routes should follow this pattern:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
like image 159
johnwp Avatar answered Oct 17 '22 01:10

johnwp