Node Express.js 4路由无法正常工作,获得404

时间:2015-06-02 06:15:42

标签: javascript node.js express webstorm

我一直试图在我的快递应用中调试我的路线。请求未定义但不确定原因。

我使用Express 4.0但运行Express 3.0风格(没有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;

路由/的 index.js

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

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

module.exports = router;

当我在webstorm中的应用程序上运行调试时,页面会抛出404页面找不到。当我查看调试时,问题是,http请求未定义:

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

1 个答案:

答案 0 :(得分:1)

确保您的app.listen ...看起来如下

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