Node.js 如何使用Angular JS中的ng view将用户会话加载到所有EJS模板?
这是我的用户模式 usermodel.jsNode.js 如何使用Angular JS中的ng view将用户会话加载到所有EJS模板?,node.js,angularjs,express,web,ejs,Node.js,Angularjs,Express,Web,Ejs,这是我的用户模式 usermodel.js const userSchema = new Schema ({ user_name:String, full_name:String, user_email:String, user_pswd:String }); app.use(express.static(path.resolve(__dirname+'/views'))); app.use(session({ secre
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
在我的index.js文件中,我正在使用这些中间件
index.js
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
用户登录后,我将存储一个用户会话
authenticate.js
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
以及根据用户是否登录,在“/”
上呈现不同的文件
index.js
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
我成功地访问了index.ejs中的user.user\u name
index.ejs
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
但是,当我试图访问welcome.ejs中的user.user\u name
时,我无法访问,我了解到res.locals
用于将数据加载到我的所有ejs模板中,但这并没有发生
欢迎光临。ejs
const userSchema = new Schema
({
user_name:String,
full_name:String,
user_email:String,
user_pswd:String
});
app.use(express.static(path.resolve(__dirname+'/views')));
app.use(session({
secret:'secret-key',
resave:false,
saveUninitialized:true,
}));
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.post("/login",urlencodedParser,function(req,res){ //user authentication
user.findOne({user_name:req.body.user_name}) //finding record through username
.then(async function(result){
if(result==null){ //if record not found
alert("no user found");
res.redirect("/#!login")
}
else //if record found
{
const check=await bcrypt.compare(req.body.user_pswd,result.user_pswd); //comparing the encrypted password
if(check)
{
req.session.user=result; //storing the user in session
alert("login successful");
res.redirect('/');
}
else
{
alert("incorrect password");
res.redirect("/#!login");
}
}
})
})
app.get("/",function(req,res) //initially loading index file
{
if(!res.locals.user) //if the user is not logged in
{
app.use(express.static(path.resolve(__dirname+'/views/landing')));
res.render("./landing/index.html");
}
else{
app.use(express.static(path.resolve(__dirname+'/views/service')));
res.render("./service/index.ejs");
}
});
<html lang="en">
<head>
<script src="../lib/angular.js"></script>
<script src="../lib/angular-route.js"></script>
<script defer src="./controller/app.js"></script>
</head>
<body ng-app="mod2" ng-controller="ctrl2">
<h1>Hi <%=user.user_name%></h1>
<div ng-view></div>
</body>
</html>
var obj= angular.module('mod2',['ngRoute']);
obj.config(function($routeProvider)
{
$routeProvider
.when('/',{
templateUrl:"./partials/welcome.ejs"
})
.when('/chat',{
templateUrl:"./partials/chat.ejs"
})
.when('/news',{
templateUrl:"./partials/news.ejs"
});
});
<html lang="en">
<body>
<h1>Welcome <%=user.user_name%></h1>
</body>
</html>
欢迎
这不起作用,它没有加载数据,我遗漏了什么?多谢各位