【问题标题】:Angular route doesn't catch the url when rendering url from ServerSide (Nodejs)从服务器端(Nodejs)呈现 url 时,角度路由没有捕获 url
【发布时间】:2019-03-08 11:50:35
【问题描述】:

我是 Mean-stack Web 开发的初学者。我正在研究我的大学项目,该项目应该开发为基于 MEAN Stack 的单页应用程序。 我在 index.html 中链接了所有角度资源、css 和其他链接。登录和主页仅在 s 内编码。 问题是如果登录身份验证成功,我想在服务器端响应为主页 {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

谢谢朋友们。

【问题讨论】:

  • 在登录路由方法中使用 console.log 确保您的 if-else 工作正常
  • 一切都很好兄弟..那里没有任何问题..完美返回结果。问题是角度路由。 login.html 正在查看 index.html 文件。但是当谈到 home.html 时,我无法查看索引文件中的内容

标签: angularjs node.js single-page-application mean-stack angular-routing


【解决方案1】:

将res.render('/home'); 更改为res.render('home');

【讨论】:

  • 好点兄弟,我累了,然后它只提供基本的主页,html 文件,而无需查看 index.html 内部。 url 也不会更改为“/home”。当使用 res.render('/home');命令我给出了这个错误:无法在视图目录“C:\Users\Toshiba\Desktop\Mastercoders\views”中查找视图“500”;
  • res.redirect('/home') 怎么样?
【解决方案2】:

你应该像这样将你的模板渲染到客户端,

res.render('home');

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 2019-02-08
    • 2015-12-25
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多