Last active
July 15, 2017 15:49
-
-
Save Deuchnord/7ef86d7360404887a03288a78b96bd71 to your computer and use it in GitHub Desktop.
Using a session variable to show a message once in Node.JS/Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/', (req, res) => { | |
var msg = req.session.message; | |
req.session.message = undefined; // you must do it before rendering, instead it will just not work | |
res.render('views/index.ejs', { | |
message: msg | |
}); | |
}) | |
.post('/connect', (req, res) => { | |
// do some things :) | |
req.session.message = { | |
type: 'success', // to chose the color of the message in the view | |
content: "Yay, you're connected!" | |
}; | |
res.redirect('/'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment