title: Serving plain-text SVGs with Express.js (Solution related to Cheerio.js)
body: <p>I am trying to process SVGs prior to sending them via Express.js.  I have the following code:</p>
  
  <pre><code>app.get("/iconic/styles/:style/:base/:icon", function (req, res) {
      var style = req.params.style;
      var base = req.params.base;
      var icon = req.params.icon;
  
      var iconPath = path.join(iconFolderRoot, style, icon);
  
      svgProcessor(iconPath, base).then(function (svg) {
          //console.log(svg);  // SO: See the example output posted below
  
          res.header("Content-Type","image/svg+xml");
  
          //res.send(new Buffer(svg, 'binary')); // No difference
  
          res.send(svg);
      }).catch(function (err) {
          res.status(404).send("Error" + JSON.stringify(err, null, "  "));
      });
  });
  </code></pre>
  
  <p>Without setting the <code>res.header()</code>, I can visit the route in the browser and it behaves as expected (scales to fit).  Unfortunately, I can't embed them in an <code>&lt;img&gt;</code> tag unless the header is set.</p>
  
  <p>With <code>res.header()</code> set, I can embed the image in an <code>&lt;img&gt;</code> tag, but it doesn't behave as I would expect (static size).</p>
  
  <p>Sending the file directly with <code>res.sendFile(iconPath)</code> works correctly.  I wondered if this was because the file needed to be sent as a binary which can be seen in <code>res.send(new Buffer(svg, 'binary'))</code> but this had the same effect as <code>res.send(svg)</code>.</p>
  
  <p>The statement <code>console.log(svg)</code> outputs the following code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- Generator: Adobe Illustrator 19.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  --&gt;
  &lt;svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewbox="0 0 128 128" style="enable-background:new 0 0 128 128;" xml:space="preserve"&gt;
  &lt;style type="text/css"&gt;
          .st0{fill:#49C5B1;}
  &lt;/style&gt;
  &lt;title&gt;Icons_TEST&lt;/title&gt;
  &lt;g id="Example"&gt;
          &lt;path {{{REMOVED FOR STACKOVERFLOW POST}}}/&gt;
  &lt;/g&gt;
  &lt;/svg&gt;
  </code></pre>
  
  <p>I can think of a workaround which would be to save the file to a temp directory and using the <code>res.sendFile(tempPath)</code>, but there has to be a better way...</p>
  
  <p>Here is a sample of the response header: </p>
  
  <pre><code>HTTP/1.1 200 OK
  X-Powered-By: Express
  Content-Type: image/svg+xml
  Date: Thu, 20 Oct 2016 00:51:18 GMT
  Connection: keep-alive
  Content-Length: 3577
  </code></pre>