gpt4 book ai didi

angularjs - 当 AngularJS 作为前端时,Express 中的 session 未定义

转载 作者:搜寻专家 更新时间:2023-11-01 00:41:18 25 4
gpt4 key购买 nike

我使用 NodeJS 和 Express 编写了网络服务。服务在端口 8090 上运行。我还用 AngularJS 编写了前端并在端口 8080 上运行。

Mongo 存储了所有用户的用户名和密码

当我通过 HTML5/AngularJS 前端登录时,AngularJS 应用程序依次调用 express 的 http post 请求。用户已通过身份验证。我设置 req.session.email = 用户的电子邮件地址。

我什至能够返回并检查 AngularJS 的控制台日志,req.session.email 在 express 中设置正确

问题是我在 Express 中创建了一个名为“restrict”的身份验证函数作为中间件函数,仅当 req.session.email 未定义时才允许访问其他 get/post 请求。

但即使在设置 session 之后,当 AngularJS 应用程序调用 Express 的另一个 get/post 请求时,这个“限制”函数会阻止调用,因为它收到未定义的 req.session.email

AngularJS 和 Express 在同一台机器上。但我不认为这是问题所在。

Express 代码片段

var url = 'mongodb://127.0.0.1:5555/contacts?maxPoolSize=2';
var mongojs = require('mongojs');
var db = mongojs(url,['data']);
var dbauth = mongojs(url,['users']);
// var request = require('request');
var http = require('http');

var express = require('express');
var cookieparser = require('cookie-parser');
var app = express();



var bodyParser = require('body-parser');
var session = require('express-session');

app.use(cookieparser());
app.use(session({secret:'v3ryc0mpl!c@t3dk3y', resave: false, saveUninitialized: true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

var user_session;

app.all('*',function(req, res, next){

res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();

});

function restrict(req,res,next){


try{

if(req.session.email){

next();

}
else{

res.send('failed');
res.end();
}

}
catch(err){

res.send('failed');
res.end();

}

};



app.post('/login',function(req,res){

//removed DB function from here to make the code look simple

req.session.email = req.body.email;
req.session.password = req.body.password;

});


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


res.send(true);

});

调用 Express 函数检查 session 状态的 AngularJS 函数

var resolveFactory = function ($q, $http, $location,LoginDetails) {

var deferred = $q.defer();


$http.get("http://127.0.0.1:8090/loggedin")
.success(function (response) {
if(response == true){
deferred.resolve(true);
}
else
{
deferred.reject();
LoginDetails.setemail('');
LoginDetails.setpassword('');
$location.path("/");

}
})
.error(function (err) {
deferred.reject();
$location.path("/");
});

return deferred.promise;

};

从根本上说,我创建的 AngularJS Resolve Function 应该是成功的,但事实并非如此。它正在失败。我正在使用 live-server 在我的笔记本电脑上运行 HTML/AngularJS,并使用 nodemon 运行 Express 应用程序

最佳答案

好的!所以原因是 AngularJS 运行在不同的端口 8080

Express 在端口 8090 上运行。这意味着如果 AngularJS 调用 Express 的 API,Express 的 session 将丢失,除非 Express 允许将 session 传递给 AngularJS,并且 AngularJS 使用 { 调用 Express 的 API withCredentials: true} 参数集。以下是当 AngularJS 和 ExpressJS 在不同端口上运行时我必须进行的更改以保持 session

在 AngularJS 中确保你调用 Express 的任何 API,它应该有{withCredentials: true} 像这样

$http.get('http://expressdomainname:expressport/api',{withCredentials: true})

如果你使用 $http.post 就很明智参数 {withCredentials: true} 很重要

现在在 Express 端

确保你有这样的应用设置

app.all('*',function(req, res, next){

//Origin is the HTML/AngularJS domain from where the ExpressJS API would be called
res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');

//make sure you set this parameter and make it true so that AngularJS and Express are able to exchange session values between each other
res.header("Access-Control-Allow-Credentials", "true");
next();

});

如果您有关于此主题的问题,请随时向我提问。我花了几天时间来解决这个问题。

关于angularjs - 当 AngularJS 作为前端时,Express 中的 session 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com