【问题标题】:How to use endpoint inside another endpoint in express node js如何在快速节点js中的另一个端点内使用端点
【发布时间】:2018-04-15 08:38:36
【问题描述】:

我只是陷入了一个问题,也不知道问题是否正确。

我正在使用 ejs 模板引擎开发 node express js。

我已经创建了一些通用端点(比如 login(POST) 端点)。

现在我想创建一个新的应用程序,它应该使用这些端点并根据这些端点的结果呈现视图。

假设我有一个通用的登录端点

router.post('/user/login', function (req, res) {
  var userLoginId = req.body.username;
  var currentPassword = req.body.password;

  UserLogin.findOne({userLoginId: userLoginId, currentPassword: currentPassword}, function (err, usr) {
    if (err) {
      console.log('Error in User login');
      console.log(err);
      res.status(500).type('application/problem+json').send();
    } else {
      if (!usr) {
        console.log('Please enter valid username and password');
        res.status(404).type('application/problem+json').send();
      } else {
        res.type('application/json').status(200).send(usr);
      }
    }
  });
});

我正在使用这个 login.ejs 文件

<div class="container panel panel-primary">
    <h2 class="h2 text-center">Login</h2>
    <form class="form-horizontal" name="loginForm" method="post" action="/user/login">
        <div class="form-group">
            <label for="username" class="col-sm-offset-2 col-sm-2 control-label">UserName</label>
            <div class="col-sm-4">
                <input type="text" name="username" class="form-control" id="username" placeholder="username">
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-offset-2 col-sm-2 control-label">Password</label>
            <div class="col-sm-4">
                <input type="password" name="password" class="form-control" id="password" placeholder="Password">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-4">
                <button type="submit" class="btn btn-primary btn-">Sign in</button>
            </div>
        </div>
    </form>
</div>

现在我想使用此代码并想登录系统。

如果用户登录成功,我想重定向到仪表板页面,否则相同的登录页面。

在执行此代码时,api 会返回 JSON 响应,我如何从该响应中决定是重定向到仪表板页面还是登录页面。

如果需要更多意见,请提供帮助,请发表评论。

【问题讨论】:

  • 登录失败后为什么要重新跳转到登录页面?登录失败后,您应该接受错误并将其提供给用户,以便他可以修复它 - 无需重新加载整个页面。这是当前最先进的 UX。成功登录后(因此,如果 http-status 为 200 或者您的返回消息包含成功或其他内容),您可以重定向用户。

标签: node.js express ejs


【解决方案1】:

我不会做一个表单请求来登录,而是做一个 AJAX 请求来登录,然后使用像 jQuery 这样的东西来做 AJAX 请求和响应。或者,您可以执行以下操作。

router.get('/login', function(req, res){
  res.render('login', {});
})

router.post('/login', function (req, res) {
  var userLoginId = req.body.username;
  var currentPassword = req.body.password;

  UserLogin.findOne({userLoginId: userLoginId, currentPassword: currentPassword}, function (err, usr) {
    if (err) {
      console.log('Error in User login');
      console.log(err);
      // res.status(500).type('application/problem+json').send();
      res.status(500).render('login', { error : 'Error finding password'})
    } else {
      if (!usr) {
        console.log('Please enter valid username and password');
        // res.status(404).type('application/problem+json').send();
        res.status(404).render('login', { error : 'Please enter valid username and password'})
      } else {
        res.redirect('/dashboard');
      }
    }
  });
});

对于 login.ejs 文件,我将有条件地呈现错误。

<!DOCTYPE html>

<html>
<body>
    <div class="container panel panel-primary">
        <h2 class="h2 text-center">Login</h2>
        <%if (locals.error) { %>
            <%= error %>
        <% } %>

             <form class="form-horizontal" name="loginForm" method="post" action="/user/login">
                        <div class="form-group">
                            <label for="username" class="col-sm-offset-2 col-sm-2 control-label">UserName</label>
                            <div class="col-sm-4">
                                <input type="text" name="username" class="form-control" id="username" placeholder="username">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="password" class="col-sm-offset-2 col-sm-2 control-label">Password</label>
                            <div class="col-sm-4">
                                <input type="password" name="password" class="form-control" id="password" placeholder="Password">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-4 col-sm-4">
                                <button type="submit" class="btn btn-primary btn-">Sign in</button>
                            </div>
                        </div>
            </form>
    </div>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 2018-11-07
    • 2017-08-27
    • 2019-09-01
    • 2018-09-04
    • 1970-01-01
    • 2016-11-18
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多