簡體   English   中英

用get的express.js靜態html

[英]express.js static html with get

在我的應用程序的首頁上,用戶可以注冊一個帳戶,然后登錄。 它在首頁上表示為登錄和注冊按鈕,單擊任一按鈕時將顯示相應的表格。

如果用戶已經登錄,我想用注銷按鈕替換這兩個按鈕,但是我需要先告知客戶。

在我的index.js中,我正在像這樣提供靜態html

app.use(express.static('public'));

我以為我可以做到以下幾點

app.get('/', function(req, res) {
    // inform the client if req.user isn't null
});

但是永遠不會調用回調

嗯! 我想會有一個登錄/注冊表格,所以必須有兩條路線,一條是.get() ,第二條是.post()

app.get('/', function(req, res) {
    // This is for navigation to the home page
}).post('/', function(req, res){
    // inform the client here about the req.body.user
});

我想你已經設置了:

app.use(bodyParser.urlencoded({extended:true})); // for "formurlencoded"
app.use(bodyParser.json()); // for "application/json"

如果不是,則必須首先加載此模塊require('body-parser') ,並且僅在使用express 4時才需要它。

我找到了解決方案。

在我的index.js中,我有這個

app.get('/isloggedin', function(req, res) {
    res.json({ loggedin: (req.user != null) });
});

然后我可以發送對/ isloggedin的獲取請求並處理結果

$.get('/isloggedin', {}, function(data) {
    if (!data.loggedin) {
        // remove logged-in only elements
    } else {
        // remove logged-out only elements
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM