Search code examples
javascriptnode.jsexpresswebstorm

Node Express.js 4 Route Not working, Getting 404


I've been trying to debug my route in my express app. The request is undefined but not sure why.

I'm using Express 4.0 but running it Express 3.0 style (no bin/www).

enter image description here enter image description here enter image description here

server.js

    var app = require('./app')

    var port = process.env.PORT || 3000;

    app.set('port', port);

app.listen(app.get('port', function(){
    console.log('Express web server is listening on port ' + app.get('port'));
}));

app.js

var express = require('express'),
    path = require('path'),
    routes = require('./routes/index'),
    app = express();

app.use('/', routes);

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(express.static(path.join(__dirname, 'public')));

module.exports = app;

routes/index.js

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

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

module.exports = router;

When I run debug on my app in webstorm, the page throws a 404 page not found. When I looked at the debug, here's the problem, the http request is undefined:

enter image description here enter image description here enter image description here enter image description here


Solution

  • Make sure your app.listen... looks as follows

    app.listen(app.get('port'), function() {
        console.log('Express web server is listening on port ' + app.get('port'));
    });