2014-10-06 93 views
0

我是node.js中的絕對新手,我嘗試通過此技術創建一個新項目。我使用快速框架,但一開始我有點麻煩。我已經解決了這個麻煩解決方法,但我對接下來的行爲問題: 我app.jsapp.get不適用於基本路由

var express = require('express') 
    , routes = require('./routes/index') 
    , routes = require('./routes/login'); 

var app = module.exports = express.createServer(); 
console.log(app.env); 
// Configuration 

app.configure(function(){ 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'ejs'); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/public')); 
}); 


app.configure('development', function(){ 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function(){ 
    app.use(express.errorHandler()); 
}); 

// Routes 

app.use(function(req, res, next) { 
    if (req.url == '/') { 
     res.render('index', { title: 'Express!' }) 
    } else { 
     next(); 
    } 
}); 
//app.get('/', routes.index); 

app.get('/login', routes.login); 


app.listen(3000, function(){ 
    console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); 
}); 

在塊//路線你可以看到app.use應用。得到。如果我嘗試使用app.get而不是app.use我得到錯誤「無法獲得/」。我試圖把index.html文件放到我的公共文件夾中。但fot「/」路線我每次得到這個文件,而不是呈現index.js。

app.get('/ login',routes.login); - 工作正常,但「/」路線有問題。我不想讓我的代碼處於這種狀態,請幫我理解這種行爲。

提前致謝。

+1

你重新聲明'routes',只是改變變量名。 – 2014-10-06 09:26:14

+0

謝謝。但如何編寫漂亮的代碼?我有每次重複對:routes = require('./ routes') - > app.get('/',routes.index); ....... routes1 = require('./ routes/login') - > app.get('/ login',routes1.login);等等。? – kimonniez 2014-10-06 09:28:45

+0

您可能會使用許多模塊化模式。例如,您可能將所有單個路線包含在一個通用的'路線'模塊中。 – 2014-10-06 09:37:01

回答

2

就像用戶PA一樣。提到,你的代碼永遠不會找到/網址,原因是因爲你重新聲明你的路由變量:

var express = require('express') 
    , routes = require('./routes/index') // first declaration of 'routes' 
    , routes = require('./routes/login'); // re-declaration of 'routes' 

這使得你的第一個路線聲明(這是指向/索引聲明)無法訪問您的代碼,這這就是爲什麼你會得到「無法得到/」的錯誤,因爲你的路由變量只能指向./routes/login。

有幾種方法可以解決這個問題,清理您的代碼:

1.指定爲不同的路線不同的變量:

var express = require('express') 
    , index = require('./routes/index') 
    , login = require('./routes/login'); 

- 0R -

2.將多個函數放入路由文件中:

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// in your routes/index file 

exports.index = function(req, res){ 
    res.render('index', { title: 'Index Page' }); 
}; 

exports.login = function(req, res){ 
    res.render('login', { title: 'Login Page' }); 
}; 


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// in your app.js file 

// import your 'routes' functions from your 'routes' file 
var express = require('express') 
     , routes = require('./routes/') 


// now use your routes functions like this: 
app.get('/', routes.index); 
app.get('/login', routes.login); 

- 或 -

在一個大型的應用程序,或者更好的代碼可維護性,你可能要打破你不同的路由功能集成到不同的文件中(而不是把所有的路由功能,在一個文件中,像上面的例子),所以使用快速默認設置作爲一個例子,它們被放置其user函數及其index功能的路由夾此類似:

routes/
    user.js 
    index.js 

然後,將它們設置他們的應用程序等這樣的:

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

並調用這些函數是這樣的:

app.get('/', routes.index); // calls the "index" function inside the routes/index.js 
app.get('/users', user.list); // calls the "list" function inside the routes/user.js file 

希望這有助於。

快速提示:app.use()用於創建中間件,這是一個將被調用的應用程序中的每個請求,給你的,開發人員,訪問請求對象req功能,並響應對象res,和以某種方式改變或增強您的應用程序的能力。 「在請求中間行動」的能力是一個強大的功能,並且它在您的原始示例中爲您工作的原因是因爲當調用/的請求時,您的app.use()中間件被調用,即使您的應用程序找不到/,當您重新聲明routes變量時丟失了,您仍在向/發出請求,您的app.use()能夠看到該請求(因爲中間件在每個請求上都會被調用,即使是「壞」的),所以您的中間件仍然看到[無效]請求/和總是這樣:

// This middleware will get called on every request, 
// even on the invalid request for '/' 
app.use(function(req, res, next) { 

    // this line of code will see that the 
    // request is for "/" and fire 
    if (req.url == '/') { 
     // the page now appears to render, because you are forcing the 
     // res.render code inside of your middleware, which isn't always 
     // recommended, but it is working for you in this example 
     // because its the only place in your example that can do anything 
     // when the '/' request is made 
     res.render('index', { title: 'Express!' }) 
    } else { 
     // this is common middleware design pattern, the next() 
     // function tells the express framework to "move on to the next function" 
     // in the middleware stack 
     next(); 
    } 
}); 
+0

謝謝你的詳細解答。 – kimonniez 2014-10-06 10:35:33