提交到localhost Express服务器时获取“ERR_EMPTY_RESPONSE”

时间:2018-03-24 19:38:48

标签: javascript express routes

我正在Express中构建一个小型身份验证应用程序。当我按下登录按钮时,应用程序挂起,然后我收到上面的错误。以下是登录pug文件的代码。

extends layout

block content
.main.container
.row
  .col-md-6.col-md-offset-3
    h1.display-4.m-b-2 Log In
    form(method='POST' action='/login')
      div.form-group
        label(for='email') Email
        input.form-control(type='text' id='email' placeholder='email'     name='email')
      div.form-group
        label(for='password') Password
        input.form-control(type='password' id='password' placeholder='password' name='password')
      button.btn.btn-primary(type='submit') Log in

任何帮助将不胜感激。我只能得出结论,由于某些原因,它不会转发到个人资料页面。下面是我的索引文件中的POST路由代码。

// POST /login
router.post('/login', function(req, res, next) {
    if (req.body.email && req.body.password) {
        User.authenticate(req.body.email, req.body.password, function(error, user) {
            if (error || !user) {
                var err = new Error('Wrong email or password.');
                err.status = 401;
                return next(err);
            } else {
                req.session.userId = user._id;
                return res.redirect('/profile');
            }
        });
    } else {
        var err = new Error('Email and password are required.');
        err.status = 401;
        return next(err);
    }
});

以下是获取个人资料的代码。不确定路线是否是拼写错误。我的哈巴狗文件是profile.pug

 // GET /profile
 router.get('/profile', function(req, res, next) {
 if (!req.session.userId) {
 var err = new Error('You are not authorized to view this page.');
 err.status = 403;
  return next(err);
 }
 User.findById(req.session.userId).exec(function(error, user) {
  if (error) {
  return next(error);
   } else {
  return res.render('profile', {
    title: 'Profile',
    name: user.name,
    favorite: user.favoriteBook
   });
  }
 });
});

0 个答案:

没有答案