使用Express-Sessions处理会话

时间:2016-07-15 07:50:54

标签: angularjs node.js session express express-session

我正在使用nodeJS。我在AWS ec2服务器中有一个网页(使用AngularJS),它从nodeJS(Express)服务器获取信息。我试图在用户登录后继续记录我的用户,即使他们刷新了。我已经读过,我可以使用快速会话来做到这一点。这是我在客户端的代码:

$http({
method: 'GET',
url: 'http://ec2-bla-bla-bla.compute.amazonaws.com:8080/verifySession'
}).then(function successCallback(response) {
   console.log(response);
}, function errorCallback(response) {

});

//And here i have the http post method that call login webservice

这是我的服务器代码:

var express = require('express');
var bodyParser = require('body-parser');
var path =  require("path");
var session = require('express-session');
var app = express();
var loginManual = require('./model/ws_package/loginManual.js');

var sess;

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,    Content-Type, Accept");
next();
});
app.use(session({secret: 'ssshhhhh'}));
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
extended: true
})); 

app.use(express.static(path.join(__dirname + '/view'))); 

app.get('/verify',function(req,res){

   sess=req.session;
   if(sess.email){
    res.send({state:1,email: sess.email});
   }
   else{
    res.send({state:-1,email: "NE"});
   }
});



app.post("/login",function(req, res)
{
var userEmail; //here is myuseremail
//I have some login code here and if it is successful:              
sess = req.session;
sess.email=userEmail;
res.send(response);
}               
);

它不能正常工作,但是当我测试/登录和/验证webservices时,直接在我的浏览器中编写url,它就可以了。我发现的另一件事是,每当我从angular创建一个新的$ http方法时,我的会话ID就会改变,所以我不知道这是否会影响我。我怎么解决这个问题?对不起我的英文,我是哥斯达黎加人!谢谢!

0 个答案:

没有答案