nodejs过滤input

在PHP中筛选input数据我使用函数htmlspecialchars和mysql_real_escape_string。 在nodejs中有没有这样的function?

我需要在我的rounter函数中检查所有的input,以防止像xss这样的黑客攻击。 谢谢!

节点validation器是完美的库,它有许多validation和卫生/过滤function,例如:

entityDecode() //Decode HTML entities entityEncode() xss() //Remove common XSS attack vectors from text (default) xss(true) //Remove common XSS attack vectors from images 

要么

 contains(str) notContains(str) regex(pattern, modifiers) //Usage: regex(/[az]/i) or regex('[az]','i') notRegex(pattern, modifiers) len(min, max) //max is optional isUUID(version) //Version can be 3 or 4 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier isDate() //Uses Date.parse() - regex is probably a better choice isAfter(date) //Argument is optional and defaults to today isBefore(date) //Argument is optional and defaults to today isIn(options) //Accepts an array or string 

Google Caja HTML清理程序有一个NodeJS包。 或者你使用这里提供的答案:

 function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&amp;") .replace(/</g, "&lt;") .replace(/>/g, "&gt;") .replace(/"/g, "&quot;") .replace(/'/g, "&#039;"); } 

对于SQL,它取决于你正在使用的库,但其中大多数将会逃避参数化查询。