6

I would like to know how to serve an svg file using Express.

Here is what I have attempted so far:

svg file

<svg width="400" height="180">
  <g>
    <rect x="50" y="20" rx="20" ry="20" width="150" height="150"
      style="fill:red;stroke: black;stroke-width:5;opacity:0.5"></rect>
  </g>
</svg>

route file

var express = require('express');
var router = express.Router();

router.get('/myRoute', function (req, res, next) {
  res.setHeader('Content-Type', 'image/svg+xml');
  res.sendFile('../views/status.svg');
});

module.exports = router;

But when I point my browser to that route, I get the following error:

This page contains the following errors:

error on line 1 at column 103: Opening and ending tag mismatch: link line 0 and head
Below is a rendering of the page up to the first error.

I don't know why this is not working and not sure where "line 1 at column 103" is pointing to. There is no such line and column in my codebase.

Any suggestions?

mc9
  • 6,121
  • 13
  • 49
  • 87

1 Answers1

2

try send svg to view but no problem

res.sendFile('../views/status.svg');

Use absolute link for sendFile

res.sendFile(__dirname + '/views/status.svg');
trquoccuong
  • 2,857
  • 2
  • 20
  • 26
  • 1
    Yep. I needed an absolute path as an argument. Did this: `res.sendFile(path.join(__dirname, '../views/status.svg'));` – mc9 Sep 13 '15 at 06:25
  • Now I realize that I need to render it first. I've posted a separate question. Care to make a suggestion? http://stackoverflow.com/questions/32546967/render-svg-file-and-serve-it-using-express – mc9 Sep 13 '15 at 06:32