2017-02-21 2 views
0

Ich versuche, Abfrage nach 'status.html' wie localhost:3000/status.html?channel="string that I want to get" zu bekommen.Versuch, Abfrage in App.get-Funktion mit Express in node.js

Also schrieb ich den Code wie folgt.

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'); 

    } 
    } 
}); 

Es ist jedoch auch dann nicht durch app.get '/status.html' ...

Was ist mein Problem hier ...? Kann mir hier jemand helfen ..?

Danke

Nur für den Fall, mein Verzeichnis wird wie unten

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

Antwort

1

app.get(path, callback) ist eine Route

ein Pfad ein string ein 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'); 
     } 
     } 
    }); 
sein kann

in deiner html-datei musst du cre aß einen Anker-Tag mit href

html-Datei

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

, wenn Sie die Statustaste klicken, wird es die /status Route app.get()

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

Vielen Dank für Ihre Antwort! :) Hm .. Ich sehe .. Ich habe bereits eingestellt und Redirect zu "localhost: 3000/status.html" aus dem Code und arbeitete ohne Probleme, also dachte ich, ich kann app.get ('/ status.html' verwenden. ..) Sache ....: '( – paulc1111

+0

'localhost: 3000/status.html' bedeutet, dass Sie eine HTML-Datei statisch laden, siehe express 'res.sendFile()' Funktion –

+0

Danke! :) Ich schaue mir nochmal die res.sendFile() Funktion an :) Nochmals Danke für die Hilfe! – paulc1111

0

Bitte entfernen Sie die Datei status.html aus dem öffentlichen Verzeichnis ruft und erstellen Sie Route, im Grunde ist es an Ihre öffentliche Verzeichnis anfordern, Wenn Sie status.html im öffentlichen Verzeichnis verwenden möchten, können Sie Ajax arbeiten dort implementieren.

+0

Vielen Dank für Ihre Antwort! :) Wie kann ich mit Ajax ...? : "( – paulc1111