我可以使用带有passport.js的busboy吗?

时间:2018-02-22 20:51:09

标签: express passport.js busboy

我在我的React应用程序中使用FormData()。我打算用它进行注册和登录。我有一个工作的passport.js注册代码片段,并且它将与busboy一起使用它,但看起来它只要有一个就一个一个地读取字段,似乎我无法在Account.register中使用它。

我已经在busboy.on('字段')中插入了Account.register,但后来意识到它不会工作。我没有在原始代码中更改req.body.username等,忽略它们。

busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
    console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    Account.register(new Account({ nickname: req.body.username, email: req.body.email }), req.body.passwordOne, (err, user) => {
      if(err){
        helpers.errors(err, res);
      } else {
        helpers.registerSuccess(res);
      }
    });
  });

  busboy.on('finish', function() {
    console.log('Done parsing form!');
    //res.writeHead(303, { Connection: 'close', Location: '/' });
    res.end();
  });

1 个答案:

答案 0 :(得分:0)

我在passport.js中使用昵称而不是用户名,因为我使用电子邮件作为用户名字段。工作代码:

router.post('/register', (req, res, next)=>{
  var busboy = new Busboy({ headers: req.headers });

  let nickname = '';
  let email = '';
  let password = '';

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
    file.on('data', function(data) {
      console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    });
    file.on('end', function() {
      console.log('File [' + fieldname + '] Finished');
    });
  });

  busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
    console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    if(fieldname == 'username') { nickname = val; }
    if(fieldname == 'email') { email = val; }
    if(fieldname == 'password') { password = val; }

  });

  busboy.on('finish', function() {
    console.log('Done parsing form!');
    console.log('email: ' + email + 'password: ' + password + 'username: ' + nickname);
    Account.register(new Account({ nickname: nickname, email: email }), password, (err, user) => {
      if(err){
        helpers.errors(err, res);
      } else {
        helpers.registerSuccess(res);
      }
    });
  });

  req.pipe(busboy);
});