Ron I Ron I - 1 year ago 146
Node.js Question

Node 101: app.use ReferenceError [route] not defined

I am new to Node/Express, and have a basic question about routing as I am getting the following error message:

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 crashed - waiting for file changes before starting...


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

File structure:

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


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

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

module.exports = router;

Additionally, I wanted to know if I should use the 'extends layout' in my dashboard.jade file, or if I should define the layout in the route


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

block content
include ./nav/nav_layout.jade
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

Answer Source

Make sure you require your edu module in your app.js file. Usually at the top of the file you would need something like var edu = require('./routes/edu.js'); (which asks node to load your edu.js file in the routes directory).

Concerning your second question, the way you make your extends call in your Jade file should be OK. For more clarity and to avoid mistakes, you should reference it by its filename, like extends ./layout.jade (if layout.jade is in the same directory as dashboard.jade)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download