Skip to content

Instantly share code, notes, and snippets.

@Deuchnord
Last active July 15, 2017 15:49
Show Gist options
  • Save Deuchnord/7ef86d7360404887a03288a78b96bd71 to your computer and use it in GitHub Desktop.
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
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