Web应用程序node.js给出错误-ERR_EMPTY_RESPONSE

时间:2018-08-19 06:56:58

标签: node.js sequelize.js passport.js postgresql-9.5

我遇到了一个巨大的问题和稀释,我正在使用Express + Postgres + secelise +护照制作一个Web应用程序。问题是在注册时,我向数据库表中添加了一个新用户,但给出了页面加载错误,并且不报告已接收电子邮件,依此类推,授权也无法加载页面然后给出err- ERR_EMPTY_RESPONSE。在路由器中,我使用2种不同的方式发送post('signin'...)和post('signup'),尝试许多不同的方式,但无法赢得错误(((((。 server.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const db = require('./db');
const morgan = require('morgan');
const flash = require('connect-flash');
require('./passport');

app.use(bodyParser.urlencoded({extended: false}));
app.use(session({
  secret: 'oursercret',
  resave: true,
  saveUnitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(morgan('dev'));
app.use(require('./routes'));

app.set("view engine", "ejs");
app.set("views", "./views");

app.listen(8000, function(err){
  if(!err){
    console.log('OK');
  } else {
    console.log('WTF?');
  }
});

passport.js

const passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var { User } = require('./db');



  passport.serializeUser(function(user, done){
    done(null, user.id);
  });

  passport.deserializeUser(function(id, done){
    User.findById(id, function(err, user){
      done(err, user);
    });
  });

passport.use('signin', new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    async (email, password, done) => {
        try {
            const user = await User.findOne({
                where: { email }
            });

            if (!user) {
                done(null, false, {
                    message: 'Incorrect email'
                    // Better
                    // message: 'Incorrect email or password'
                });

                return;
            }

            if (user.password !== password) {
                done(null, false, {
                    message: 'Incorrect password'
                    // Better
                    // message: 'Incorrect email or password'
                });

                return;
            }

            done(null, user);
        } catch (error) {
            done(error);
        }
    })
);

passport.use('signup', new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    async (email, password, done) => {
        try {
            const findUser = await User.findOne({
                where: { email }
            });

            if (findUser) {
                done(null, false, {
                    message: 'Email already taken'
                });

                return;
            }

            const user = await User.create({
                email,
                password
            });

            done(null, user);
        } catch (error) {
            done(error);
        }
    }
));

db.js

const Sequelize = require('sequelize');

var opt = {
  schema: 'kik_shema'
};

const db = new Sequelize('postgres://alejandro:Leomessi10@127.0.0.1:5434/bd', opt);

const User = db.define('kik', {// kik - название таблицы
  email: Sequelize.STRING,
  password: Sequelize.STRING,
    //username: Sequelize.TEXT,
}, {
  timestamps: false, //ЛУЧШЕ ВКЛЮЧИТЬ, ПОКАЗЫВАЕТ КОГДА БЫЛ СОЗДАН ОБЪЪЕКТ И КОГДА ОБНОВЛЯЛСЯ
});

module.exports = {
  User,
  db
};

roures.js

const passport = require('passport');
const { Router } = require('express');
const router = new Router();

router.get('/signup', (req, res) => {
    res.render('signup');
});

router.get('/signin', (req, res) => {
    res.render('signin');
});

router.get('/index', (req, res) => {
    res.render('index');
});

router.post('/signin', function(req, res, next) {
  passport.authenticate('signin', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/signin'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/users/' + user.username);
    });
  })(req, res, next);
});


router.post('/signup', passport.authenticate('signup', {
       //successRedirect: '/index', //из passport.use берется signup
       failureRedirect: '/signup',
       failureFlash: false
     }), function(req, res, next){
       res.redirect('/index');
     });

module.exports = router;

0 个答案:

没有答案