我想知道如何使用 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/