forked from 3drepo/3drepo.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-test.js
32 lines (24 loc) · 946 Bytes
/
cookie-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var express = require('express');
var bodyParser = require('body-parser'); // for reading POSTed form data into `req.body`
var expressSession = require('express-session');
var cookieParser = require('cookie-parser'); // the session is stored in a cookie, so we use this to parse it
var app = express();
// must use cookieParser before expressSession
app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere'}));
app.use(bodyParser());
app.get('/', function(req, res){
var html = '<form action="/" method="post">' +
'Your name: <input type="text" name="userName"><br>' +
'<button type="submit">Submit</button>' +
'</form>';
if (req.session.userName) {
html += '<br>Your username from your session is: ' + req.session.userName;
}
res.send(html);
});
app.post('/', function(req, res){
req.session.userName = req.body.userName;
res.redirect('/');
});
app.listen(80);