简体   繁体   中英

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

在此处输入图片说明在此处输入图片说明在此处输入图片说明

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:

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

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'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM