简体   繁体   English

对于有效的电子邮件地址正则表达式,NodeJS返回无效

[英]NodeJS returns invalid for valid email address regex

EDIT NodeJS route handler 编辑 NodeJS路由处理程序

// require() statements above
let error = {};

module.exports = {
  authorize: (req, res, next) => {
    const USERNAME  = req.body.username,
          PASSWORD  = req.body.password,
          SCOPES    = req.body.scopes;

    console.log(req.body);

    const SCOPE_LOOKUP = ['read', 'write', 'admin'];
    if(!VALIDATE_EMAIL(USERNAME)) {
      error.message = 'Invalid username.';
    }

    if(error.message) { return next(error) };
    return res.status(200).json(req.body);
  }
};

The code below runs on a NodeJS application I am working on. 下面的代码在我正在处理的NodeJS应用程序上运行。 The email address const is populated with the contents of req.body.email and I am using Postman to make the API calls. 电子邮件地址const中填充了req.body.email的内容,我正在使用Postman进行API调用。

Running the code below and passing a valid email address will work as expected. 运行下面的代码并传递有效的电子邮件地址将按预期工作。 However if I pass an invalid email address the code also works as expected, but when I pass in another valid email address I end up with Invalid email . 但是,如果我传递了一个无效的电子邮件地址,该代码也可以按预期方式工作,但是当我传递另一个有效的电子邮件地址时,我最终将收到Invalid email This occurs with no restart of the server. 这种情况不会重新启动服务器。

Is there an issue with execution order or scope, which I have missed? 我错过了执行顺序或范围的问题吗?

 const VALIDATE_EMAIL = email => { const EXP = /^(([^<>()[\\]\\\\.,;:\\s@"]+(\\.[^<>()[\\]\\\\.,;:\\s@"]+)*)|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/; const DOMAIN = '@example.com'; const OUTPUT = (EXP.test(email) && email.indexOf(DOMAIN, email.length - DOMAIN.length) !== -1) ? true : false; return OUTPUT; }; (() => { let error = {}; const EMAIL = 'joebloggs@example.com'; if(!VALIDATE_EMAIL(EMAIL)) { error.message = 'Invalid email.'; } if(error.message) { console.log(error.message); return }; console.log(EMAIL); })(); 

Your problem is that you're persisting your error message throughout the lifecycle of your application. 您的问题是您在应用程序的整个生命周期中都保留了错误消息。 Don't declare the error object outside the scope of the handler... You need to declare the error object within the request handler so that each request has a fresh error object (and subsequent error message). 不要在处理程序范围之外声明error对象...您需要在请求处理程序中声明error对象,以便每个请求都有一个新的error对象(以及后续的错误消息)。

module.exports = {
  authorize: (req, res, next) => {
    const error = {
      message: '',
      something: '',
      foo: ''
    };

    const USERNAME  = req.body.username,
          PASSWORD  = req.body.password,
          SCOPES    = req.body.scopes;

    console.log(req.body);

    const SCOPE_LOOKUP = ['read', 'write', 'admin'];
    if(!VALIDATE_EMAIL(USERNAME)) {
      error.message = 'Invalid username.';
    }

    if(error.message) { return next(error) };
    return res.status(200).json(req.body);
  }
};

On principle, don't ever do what you're doing (though it seems to work).. Use a library like email-addresses . 原则上,永远不要做您正在做的事情(尽管它似乎可以工作)。使用诸如email-addresses之类的库。

npm install email-addresses;

const parseEmail = require('email-addresses').parseOneAddress;
let parseResult = parseEmail(EMAIL);
console.log(parseResult);

That will output... 那将输出...

{ parts: 
   { name: null,
     address: 
      { name: 'addr-spec',
        tokens: 'joebloggs@example.com',
        semantic: 'joebloggs@example.com',
        children: [Object] },
     local: 
      { name: 'local-part',
        tokens: 'joebloggs',
        semantic: 'joebloggs',
        children: [Object] },
     domain: 
      { name: 'domain',
        tokens: 'example.com',
        semantic: 'example.com',
        children: [Object] } },
  name: null,
  address: 'joebloggs@example.com',
  local: 'joebloggs',
  domain: 'example.com' }

So if you want if you need the domain, get parseResult.domain 因此,如果您需要域,请获取parseResult.domain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM