简体   繁体   English

Node Express.js 4路由不起作用,获取404

[英]Node Express.js 4 Route Not working, Getting 404

I've been trying to debug my route in my express app. 我一直在尝试在我的Express应用中调试路线。 The request is undefined but not sure why. 该请求未定义,但不确定原因。

I'm using Express 4.0 but running it Express 3.0 style (no bin/www). 我使用的是Express 4.0,但运行的是Express 3.0样式(无bin / www)。

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

server.js 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 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 路由/ 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. 当我在Webstorm中对我的应用程序运行调试时,页面抛出未找到的404页面。 When I looked at the debug, here's the problem, the http request is undefined: 当我查看调试时,这是问题所在,http请求未定义:

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

Make sure your app.listen... looks as follows 确保您的app.listen ...如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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