mc9
mc9

Reputation: 6349

Serve svg file using express

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?

Upvotes: 6

Views: 12770

Answers (1)

trquoccuong
trquoccuong

Reputation: 2873

try send svg to view but no problem

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

Use absolute link for sendFile

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

Upvotes: 2

Related Questions