节点101:app.use ReferenceError [route]未定义

时间:2016-06-22 16:32:30

标签: javascript node.js express pug

我是Node / Express的新手,并且有一个关于路由的基本问题,因为我收到以下错误消息:

app.use('/edu', edu);
                ^
ReferenceError: edu is not defined
at Object.<anonymous> (/Users/ronitelman/Dropbox/happy/happy/app.js:29:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/ronitelman/Dropbox/happy/happy/bin/www:7:11)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)

[nodemon] app崩溃 - 在开始之前等待文件更改...

app.js:

app.use('/', routes);
app.use('/users', users);
app.use('/about', about);
app.use('/edu', edu);

文件结构:

> root
  > ...
  > routes
    | about.js
    | edu.js
    | index.js
    | users.js

edu.js:

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

router.get('/', function(req, res) {
  res.render('dashboard', { layout: 'layout_edu' });
});

module.exports = router;

此外,我想知道我是否应该在dashboard.jade文件中使用'extends layout',或者我是否应该在路径中定义布局

dashboard.jade

extends layout //- or   res.render('dashboard', { layout: 'layout_edu' }); ?

block content
  include ./nav/nav_layout.jade
  .wrapper
    include ./home/home.jade
    include ./home/benefits.jade      
    include ./home/students.jade
    include ./home/teachers.jade    
    include ./home/admin.jade
    include ./home/analytics.jade                             
    include ./home/tech.jade
    include ./home/featured.jade

1 个答案:

答案 0 :(得分:1)

确保您的edu文件中需要app.js模块。通常在文件的顶部,您需要类似var edu = require('./routes/edu.js');的内容(要求节点在edu.js目录中加载routes文件。)

关于您的第二个问题,您在Jade文件中进行extends调用的方式应该没问题。为了更清晰并避免错误,您应该通过其文件名引用它,例如extends ./layout.jade(如果layout.jadedashboard.jade在同一目录中)