-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
69 lines (60 loc) · 1.54 KB
/
server1.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var path = require('path');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.use(session({secret: 'ssshhhhh',
resave: false,
saveUninitialized: false}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
var sess;
app.get('/',function(req,res){
sess = req.session;
console.log("hi this for testing");
//console.log(sess);
//Session set when user Request our app via URL
//if(sess.email) {
/*
* This line check Session existence.
* If it existed will do some action.
*/
res.redirect('/admin');
/*}
else {
res.render('index.html');
}*/
});
app.post('/login',function(req,res){
sess = req.session;
//In this we are assigning email to sess.email variable.
//email comes from HTML page.
sess.email=req.body.email;
res.end('done');
});
app.get('/admin',function(req,res){
sess = req.session;
console.log(sess);
//console.log('Cookies: ', req.cookies);
if(sess.email) {
res.write('<h1>Hello '+sess.email+'</h1>');
res.end('<a href="/logout">Logout</a>');
} else {
res.write('<h1>Please login first.</h1>');
res.end('<a href="/">Login</a>');
}
});
app.get('/logout',function(req,res){
req.session.destroy(function(err) {
if(err) {
console.log(err);
} else {
res.redirect('/');
}
});
});
app.listen(5000);