I have a Node application running express and I want to escape apostrophes on the incoming request body properties, but I'm not confident this is the best way to do it.
exports.p_edit = function(req, res) {
var args = [
req.body.title.split("'").join("\\'"),
req.files.thumbnail.name.split("'").join("\\'"),
req.body.tags.split("'").join("\\'"),
req.body.topic.split("'").join("\\'"),
req.body.preview.split("'").join("\\'"),
req.body.markdown.split("'").join("\\'")
];
// ...
// Do other stuff
I know I can use a for(prop in req.body) loop to iterate over the all request body properties, but how can I reassign the values dynamically to each property?
For example if I do
for(prop in req.body) {
prop.split("'").join("\\'");
}
and later reference req.body.title (or any property), then the value is unchanged. Similarly, if I try to assign it like req.body.prop = prop.split("'").join("\\'"); (hoping that req.body.prop would change dynamically with each iteration) it simply adds the prop property to the req.body object. How can I dynamically iterate and reassign these properties?