简体   繁体   中英

NodeJS returns invalid for valid email address regex

EDIT NodeJS route handler

// 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. The email address const is populated with the contents of req.body.email and I am using Postman to make the API calls.

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 . 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).

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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