2017-02-21 27 views
0

我試圖在'status.html'之後獲得查詢,如localhost:3000/status.html?channel="string that I want to get"嘗試使用node.js中的express在app.get函數中獲取查詢

所以我寫了下面的代碼。

app.get('/status.html', isLoggedIn, function(res, req){ 
    if (req.query.channel){ 
    if (req.isAuthenticated()){ 
     res.redirect('/status'); 

    }else{ 
     console.log("Please Log in to access to this webpage"); 
     res.redirect('/login'); 

    } 
    } 
}); 

然而,它甚至不經過app.get '/status.html' ......

什麼這裏是我的問題...? 任何人都可以幫我在這裏..?

謝謝

以防萬一,我的目錄設置如下

node_modules 
public 
    images 
    javascripts 
    js 
    stylesheets 
    status.html 
routes 
views 
    login 
app.js 
package.json 

回答

1

app.get(path, callback)是一個路由

的路徑可以是string一個a regex pattern

// if you use `/status.html` then html `href` should as be same path 
// `/status.html` 
// app.get('/status.html', isLoggedIn, function(res, req){ 

app.get('/status', isLoggedIn, function(res, req){ 
     if (req.query.channel){ 
     if (req.isAuthenticated()){ 
      res.redirect('/status'); 

     }else{ 
      console.log("Please Log in to access to this webpage"); 
      res.redirect('/login'); 
     } 
     } 
    }); 

在你的html文件中你必須創建吃得href

HTML文件中的錨標記

// <a href ="/status.html" class="btn btn-default btn-sm">Status</a> 
<a href ="/status" class="btn btn-default btn-sm">Status</a> 

當您單擊狀態按鈕,它會調用/status路線app.get()

// `/status.html` is just a route path it has nothing to do with html files 
+0

謝謝你回覆我! :)嗯..我看到..我已經設置並重定向到'localhost:3000/status.html'從代碼和工作沒有問題,所以我想我可以使用app.get('/ status.html'。 ..)的東西....:'( – paulc1111

+0

'localhost:3000/status.html'意味着你正在靜態加載一個html文件, 快看看'res.sendFile()'函數 –

+0

Thanks :)我會看看express res.sendFile()函數:)再次感謝您的幫助! – paulc1111

0

請從您的公共目錄刪除該文件status.html並創建路由,基本上它是請求你的公共目錄, 如果你想在公共目錄中使用status.html,你可以在那裏實現一個Ajax工作。

+0

謝謝你回覆我! :)我怎麼能用阿賈克斯...? :'( – paulc1111

相關問題