我是Mean-stack web开发的初学者。我正在开展我的大学项目,该项目应该开发为基于MEAN Stack的单页面应用程序。我链接了index.html中的所有角度资源,css和其他链接。登录和主页仅在s内编码。问题是如果登录验证成功,我想在服务器端作为Home {res.render('/ home')}的url响应时使用角度路由显示主页,但这种方式不起作用。但登录页面正常工作。任何帮助将非常感激。
这个index.html--
<link rel="stylesheet" href="css/bootstrap.min.css">
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<script src="../server/angModule.js"></script>
<script src="../server/angularRoute.js"></script>
<script src="js/controllers/loginController.js"></script>
<script src="js/controllers/homeController.js"></script>
</head>
<body ng-app="Pharmacy">
<div ng-view > </div>
</body>
这个login.html
<form action="/login" method="post">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Login</button>
<input type="checkbox" checked="checked" name="remember-me"> Remember me
</form>
这是Angular模块,路由和控制器
var app = angular.module('Pharmacy', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: '../views/login.html',
controller: 'loginController'
}).when('/home', {
templateUrl: '../views/home.html',
controller: 'homeController'
}).when('/about', {
templateUrl: '../views/about.html',
controller: 'studentController'
}).otherwise({
redirectTo: "/"
});
});
app.controller("loginController", function ($scope) {
$scope.login = 'this is login';
});
app.controller("homeController", function ($scope) {
$scope.message = 'This is home';
});
这是必要的服务器端代码;
var UH = require('./modules/userHandaling');
module.exports = function(app) {
app.get('/', function(req, res) {
res.render('index');
});
app.get('/index', function(req, res) {
res.render('index');
});
app.get('/login', function(req, res) {
if (req.cookies.user == undefined || req.cookies.pass == undefined){
res.render('index', { title: 'Hello - Please Login To Your Account' });
}else{
// attempt automatic login //
UH.autoSignIn(req.cookies.user, req.cookies.pass, function(o){
if (o != null){
req.session.user = o;
res.redirect('/home');
} else{
res.render('index', { title: 'Hello - Please Login To Your Account' });
}
});
}
res.render('index');
});
app.post('/login', function(req, res){
UH.manualSignIn(req.body['username'], req.body['password'], function(error, output){
if (!output){
res.status(400).send(error);
} else {
req.session.user = output;
if (req.body['remember-me'] == 'on'){
res.cookie('username', output.username, { maxAge: 900000 });
res.cookie('password', output.password, { maxAge: 900000 });
}
res.render('/home');
}
});
});
我也在Github上传了我的项目:https://github.com/SarasaGunawardhana/StackOverFlow-Question
谢谢各位朋友。
将res.render('/home');
改为res.render('home');
您应该像这样将模板渲染到客户端,
res.render('home');