2017-02-21 25 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

相关问题