编辑 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);
}
};
以下代码在我正在处理的NodeJS应用程序上运行。电子邮件地址const
填充了req.body.email
的内容,我使用Postman进行API调用。
运行下面的代码并传递有效的电子邮件地址将按预期工作。但是,如果我传递了无效的电子邮件地址,代码也会按预期工作,但当我传入另一个有效的电子邮件地址时,我最终会使用Invalid email
。没有重新启动服务器就会发生这种情况。
执行顺序或范围是否有问题,我错过了?
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);
})();
&#13;
答案 0 :(得分:3)
您的问题是,您在应用程序的整个生命周期中都会持久保存错误消息。不要在处理程序范围之外声明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);
}
};
对象,以便每个请求都有一个新的<?php comments_template(); ?>
对象(以及随后的错误消息)。
import Foundation
import CoreData
import UIKit
class SaveModel: NSManagedObject {
func CacheStations(){
// create an instance of our managedObjectContext
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// we set up our entity by selecting the entity and context that we're targeting
let entity = NSEntityDescription.insertNewObjectForEntityForName("CachedStations", inManagedObjectContext: moc) as! SaveModel
//add the data
entity.land = "nl";
// we save our entity
do {
try moc.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
答案 1 :(得分:1)
原则上,不要做你正在做的事情(尽管它似乎有效)..使用像这样的库。
npm install email-addresses;
const parseEmail = require('email-addresses').parseOneAddress;
let parseResult = parseEmail(EMAIL);
console.log(parseResult);
那将输出......
{ 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' }
因此,如果您需要域名,请获取parseResult.domain