2013-05-14 95 views
14

我試圖在快速應用中添加新路由,但在嘗試啓動服務器時出現錯誤。該錯誤是添加新路由到節點快車

C:\development\node\express_app\node_modules\express\lib\router\index.js:252 
    throw new Error(msg); 
     ^
Error: .get() requires callback functions but got a [object Undefined] 

這裏是我的文件,我是新來的節點,以便讓我知道如果我離開了一個重要文件,

路線/ furniture.js

exports.furniture = function(req, res){ 
    res.render('furniture', { title: '4\267pli' }); 
}; 

路線/ index.js

/* 
* GET home page. 
*/ 

exports.index = function(req, res){ 
    res.render('index', { title: '4\267pli' }); 
}; 

的意見/ furniture.ejs

<!DOCTYPE html> 
<html> 
<head> 
    <title>4&middot;pli -- architecture</title> 
    <link rel='stylesheet' href='/stylesheets/style.css'/> 
    <link href='http://fonts.googleapis.com/css?family=Didact+Gothic' rel='stylesheet' type='text/css'> 
</head> 
<body> 
<div class="wrapper"> 
    <h1 class="logo"><%= title %></h1> 
</div> 
</body> 
</html> 

app.js

/** 
* Module dependencies. 
*/ 

var express = require('express') 
    , routes = require('./routes') 
    , user = require('./routes/user') 
    , furniture = require('./routes/furniture') 
    , http = require('http') 
    , path = require('path'); 

var app = express(); 

// all environments 
app.set('port', process.env.PORT || 3000); 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 
app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.bodyParser()); 
app.use(express.methodOverride()); 
app.use(app.router); 
    app.use(require('stylus').middleware(__dirname + '/public')); 
app.use(express.static(path.join(__dirname, 'public'))); 

// development only 
if ('development' == app.get('env')) { 
    app.use(express.errorHandler()); 
} 

app.get('/', routes.index); 
app.get('/users', user.list); 
app.get('/furniture', routes.furniture); 

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

回答

24

麻煩的是:

routes = require('./routes'), 
user = require('./routes/user'), 
furniture = require('./routes/furniture'), 

這3設置你的路由文件夾,而不是特定的文件,表示將尋找一個index.js(未找到,然後 - >錯誤)

這些文件夾裏面,你應該把index.js你:

exports.xxxx = function(req, res){ 
    res.render('xx', { foo: foo}); 
}; 

然後,你的項目文件夾結構應該是這樣的:

routes/ 
    ├── index.js 
    │ 
    ├── user/ 
    │  └── index.js (with a exports.user inside) 
    │ 
    └── fourniture/ 
     └── index.js (with a exports.furniture inside) 

您可以添加多個導出功能到這樣的路線:

app.js

// a folder called routes with the index.js file inside 
routes = require('./routes') 

. 
. 
. 

app.get('/', routes.main_function); 
app.get('/sec_route', routes.sec_function); 
app.post('/other_route', routes.other_function); 

/routes/index.js

exports.main_function = function(req, res){ 
    res.render('template1', { foo: foo }); 
}; 

exports.sec_function = function(req, res){ 
    res.render('template2', { bar: bar }); 
}; 

exports.other_function = function(req, res){ 
    res.render('template1', { baz: baz }); 
}; 
+0

感謝偉大工程 – 2013-05-14 17:13:29

+0

@ 3boll什麼,如果你想有以下幾個用戶的功能。 .. index.js admin.js和foo.js ...你會怎麼寫? – Cmag 2013-12-02 01:59:02

+1

@Clustermagnet檢查添加到響應中的代碼。 – jmingov 2013-12-02 19:08:32

6

如果你的網站是如此之大一些的時候,我更願意做這樣的事情:

routes/furniture.js

module.exports = function(app) 
{ 
    app.get("/furniture/", function(req, res) { 
     res.render('furniture', { title: '4\267plieee' }); 
    }); 
} 

然後在app.js

require("./routes/furniture")(app); 

這主要是相同的,但應用程序。 js會更乾淨。

+0

中保存的user.js文件相關聯,謝謝我發現這將很快有用。 – 2013-05-15 13:01:02

+1

這絕對是一個不錯的方法,因爲它實際上保持了路由文件夾內的HTTP路由。 – 2014-08-22 10:23:36

2

儘管這已經有點舊,儘管分享我這樣做的方式。這是另一種使代碼更加清晰和易於添加路由的方法。

app.js

const app = express(); 
const routes = require('./routes'); 
app.use('/api', routes); //Main entry point 

/路由/索引。JS

const router = require('express').Router(); 
const user = require('./user'); 
const admin = require('./admin'); 

//This is a simple route 
router.get('/health-check', (req, res) => 
    res.send('OK') 
); 

router.route('/users') 
     .post(validate, user.createUser); 

router.route('/users/:userId') 
     .get(validateUser, user.getUser) 
     .patch(validateUser, user.updateUser) 
     .delete(validateUser, user.deleteUser); 

router.route('/admins/:adminId/dashboard') 
     .get(validateAdmin,admin.getDashboard); 

module.exports = router; 

「的ValidateUser」和「validateAdmin」是定製中間製品,其將被用於驗證請求參數或做一些預處理請求到達實際請求處理程序之前。這是可選的,您也可以有多箇中間件(逗號分隔)。

/routes/user.js

module.exports = { 
    createUser:function(req,res,next){ 

    }, 
    updateUser:function(req,res,next){ 

    }, 
    deleteUser:function(req,res,next){ 

    } 
} 

/routes/admin.js

module.exports = { 
    getDashboard:function(req,res,next){ 

    } 
}