Removing Newlines from req.body

I am working on a node project. In the following snippet of middleware, I need to remove the newlines from req.body to prepare it for sending inside a JSONP response.

server.use(function(req,res,next){
  if(req.query.concat) {
    req.body = req.body; // <--- HERE I need to remove the newlines, etc.
  } else {
    req.body = req.body || {};
    req.body.jsonp_callback = req.query.callback;
  }
  next();
})

How can I get the req.body ready for JSONP?

Use str.split("\n") which will return an array with your string split into chunks as denoted by the newlines, and then use a loop to piece everything back together.

var arr = str.split("\n");
var result = "";
for(var i = 0; i < arr.length; i++) {
  result += arr[index];
}

The newline character in javascript is just like any other character and it's denoted by '\n'. Use global replace:

server.use(function(req,res,next){
  if(req.url.match(/^\/(api|view|partial)/) && req.query.callback){
    if(req.query.concat) {
      req.body = req.body.replace(/\\n/g, ''); // <--- Newlines are globally replaced with empty string
    } else {
      req.body = req.body || {};
      req.body.jsonp_callback = req.query.callback;
    }
  }
 next();
})