node.js - 使用 express 服务 svg 文件

标签 node.js express svg

我想知道如何使用 Express 提供 svg 文件。

到目前为止,这是我尝试过的:

svg 文件

<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>

路由文件

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;

但是当我将浏览器指向该路线时,出现以下错误:

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.

我不知道为什么这不起作用,也不确定“第 103 列的第 1 行”指向何处。我的代码库中没有这样的行和列。

有什么建议吗?

最佳答案

尝试发送 svg 以查看但没问题

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

发送文件使用绝对链接

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

关于node.js - 使用 express 服务 svg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545584/

相关文章:

node.js - Nodejs 传递 res.end() 作为回调不起作用

node.js - AWS EC2 Angular 5 ng 服务未通过浏览器连接

javascript - node.js 表达外部图片推送到客户端

jquery - 使用 Keith Wood 的 jQuery SVG 插件对外部加载的 SVG 进行动画处理

javascript - 使用 jquery 对连接在 socket.io 中同一房间的所有用户执行相同的操作

node.js - 握手nodeJS之前套接字无法连接

express - 这两种使用 express 中间件的方法有区别吗?

javascript - express-validator - req.checkBody 不是函数

javascript - 检测 svg 元素上的回车键

php - Imagick 使用谷歌字体将 svg 转换为 png