无法读取未定义的属性get(节点js)

时间:2016-12-17 13:13:40

标签: node.js

我是节点js的新手。刚刚创建了两个文件,一个是app.js,另一个是routes.js。当我没有分离routes.js时,我的代码工作正常。但是当我包含routes.js时,然后给我"无法读取属性获取未定义"错误。

app.js:

var express = require('express'), stylus = require('stylus'), nib = require('nib')
var app = express()

function compile(str, path) {
  return stylus(str)
    .set('filename', path)
    .use(nib());
}

app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.logger('dev'))
app.use(stylus.middleware(
  { src: __dirname + '/public'  , compile: compile }
))
app.use(express.static(__dirname + '/public'))

var routes = require('./routes');
app.use('/', routes);

app.listen(8888)

routes.js:

module.exports = (function() {
    'use strict';
    var router = require('express').Router();

    router.get('/', function (req, res) {
      res.render('index',
       { title : 'Home' , content : 'this is testing content'}
      )
    })

    return router;
})();   

以下是错误:

TypeError: Cannot read property 'get' of undefined
at D:\m_node\routes.js:7:8
at Object.<anonymous> (D:\m_node\routes.js:14:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:1
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\m_node\app.js:20:14)

0 个答案:

没有答案