2012-05-17 48 views
6

總node.js的noobie,開始從各種教程和網站的演示代碼玩,我注意到的東西,我不明白...node.js的app.get不會被調用

即,如果我有指數.html在我的/公用文件夾中,然後

app.get("/", function (req, res) { 
    console.log("get /"); 
    res.redirect("/test.html"); 
}); 

根本就不會被調用。當我重新命名的index.html到index2.html然後調用方法時,我重定向到/public/test.html

這是我有:

var io = require('socket.io'), 
    express = require('express'), 
    MemoryStore = express.session.MemoryStore, 
    app = express.createServer(), 
    sessionStore = new MemoryStore(); 

app.configure(function() { 
    app.use(express.bodyParser()); 
    app.use(express.cookieParser()); 
    app.use(express.session({ 
     store: sessionStore, 
     secret: 'secret', 
     key: 'express.sid' 
    })); 
    app.use(express.static(__dirname + '/public')); 
}); 

,其餘是相當大部分來自本教程:http://www.danielbaulig.de/socket-ioexpress/

同樣的問題出現在任何其他文件中。如果我有/public/test.html,然後當我打電話

http://localhost:8201/test.html 

這app.get不叫:

app.get("/test.html", app.authenticateUser, function (req, res) { 
    console.log("get /test.html"); 
    res.redirect("/test2.html"); 
}); 

當我刪除的test.html然後我得到轉發到TEST2。 html ...

我試圖重定向的原因是,如果用戶沒有登錄我不希望他打開index.html,而是想轉發他到login.html,如果索引.html存在。唯一的「解決方案」是做它的客戶端吸引,我不希望index.html加載到客戶端瀏覽器只是爲了將他轉發到login.html,服務器應該,在我的看法,處理。

+0

爲什麼你使用靜態'index.html'而不是視圖?在我看來,你只是讓自己變得更加困難。 –

回答

1

這是因爲快遞在請求到達您的代碼之前會過濾請求。它找到該文件並將其返回給瀏覽器。

解決方案是通過socket.io發送一個事件,告訴用戶瀏覽器中的代碼將文件重定向或移動到私人空間(公共目錄之外)並通過CydGy建議的「fs」提供。

0

這個非常好的教程(http://www.danielbaulig.de/socket-ioexpress/)在套接字上進行處理。 而我認爲這種情況沒有用。

所以,看這個:

app.get("/test.html", app.authenticateUser, function (req, res) { 

但如果是app.authenticateUser? 這肯定是他誰塊

因此,通過替換它:

app.get("/test.html", function (req, res) { 

或修改app.authenticateUser

(和使用模塊fs閱讀您的文件,然後,你可以res.send(file);

不要忘記你的網址寫html的,否則,您必須通過"/test"更換"/test.html"

我希望它能幫助你。

+0

http://www.danielbaulig.de/socket-ioexpress/鏈接在我的文章中提到...這是我用的教程... 至於app.authenticateUser我刪除它,仍然是相同的.. –

16

問題是你的靜態文件中間件app.use(express.static(__dirname + '/public'))是你的路由器的「前面」。當你寫

app.configure(function() { 
app.use(express.bodyParser()); 
app.use(express.cookieParser()); 
app.use(express.session({ 
    store: sessionStore, 
    secret: 'secret', 
    key: 'express.sid' 
})); 
app.use(express.static(__dirname + '/public')); 
}); 

這相當於

app.configure(function() { 
app.use(express.bodyParser()); 
app.use(express.cookieParser()); 
app.use(express.session({ 
    store: sessionStore, 
    secret: 'secret', 
    key: 'express.sid' 
})); 
app.use(express.static(__dirname + '/public')); 
app.use(app.router); //Express implicitly appends router middleware! 
}); 

因爲快遞隱含在堆棧的末尾附加路由器中間件,如果你不加它明確地方。現在你清楚地看到靜態文件中間件在路由器前面。如果找到靜態文件,則由app.use(express.static(__dirname + '/public'))提供服務,並且從不會調用路由器app.use(app.router)。如果你想要你的請求總是通過路由器,你應該把它放在前面,例如

app.configure(function() { 
app.use(express.bodyParser()); 
app.use(express.cookieParser()); 
app.use(express.session({ 
    store: sessionStore, 
    secret: 'secret', 
    key: 'express.sid' 
})); 
app.use(app.router); //Now router comes first and will be executed before static middleware 
app.use(express.static(__dirname + '/public')); 
}); 
+0

感謝您的詳細解答,不幸的是,當我添加app.use(app.router)時,它不工作; ... –

+0

很好的解釋..... – Nav

+0

優秀的響應。 –