node.js express app找不到jade视图,404消息响应

时间:2017-04-10 17:24:28

标签: node.js express pug

对于我的应用程序,我使用node.js作为后端,使用Express作为框架。我使用Netbeans作为IDE,我得到以下输出。

Not Found
404

Error: Not Found
at /home/ingeborg/netbeans/GML/app.js:33:13
at Layer.handle [as handle_request] (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:317:13)
at /home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:335:12)
at next (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:275:10)
at /home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:635:15
at next (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:260:14)
at Function.handle (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:174:3)
at router (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:47:12)

我有以下数据设置:

- node-modules
- public
    - fonts
    - images
    - javascripts 
    - stylesheets
        stylesheets.less
        stylesheet.css
    - routes
        index.js
        news.js
        users.js
    - views
        index.jade
        error.jade
        news.jade
        layout.jade
    Gruntfile.js
    app.js
    gulpfile.js
    package.json

app.js看起来像这样

var news  = require('./routes/news');
var index = 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', 'jade');
...
app.use('/', index);
app.use('/index', index);
app.use('/news', news);
app.use('/users', users);

这是我的index.jade

h3 
  a(href="news") link

这是我的news.jade

h3 test

路由是通过路由文件实现的。 index.js

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

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

router.get('/index', function(req, res, next) {
    res.render('index');
});
module.exports = router;

news.js路由器文件是

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

/* GET home page. */
router.get('news', function(req, res, next) {
    res.render('news');
});
module.exports = router;        

以下情况

  • 链接“/”和“index”上的路由按预期工作。
  • “/ news”或“news”上的路由(在rounting,app.js和view文件中测试)没有。我不知道为什么路由不起作用。我希望我没有忘记某事。

THX, 英格

2 个答案:

答案 0 :(得分:2)

您的代码适合我。

<强> /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 index = require('./routes/index');
var news  = require('./routes/news');
var users = require('./routes/users');

var app = express();

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

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
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('/', index);
app.use('/users', users);
app.use('/news', news);

// 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
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

<强> /routes/news.js

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

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('news');
});
module.exports = router;   

答案 1 :(得分:0)

缺少新闻路线文件。在那里你必须渲染你的玉。

var news  = require('./routes/news');