Node.js 快速将 SVG 文件流保存到文件
Node.js Express save SVG file stream to file
我是 Express 的新手,我需要你的帮助。
如何使用 Express 在服务器上保存 SVG?
const qr = require('qr-image');
const fs = require('fs');
exports.qr = function (req, res) {
var code = qr.image(new Date().toString(), { type: 'svg' });
// I would like to do something like this:
fs.writeFile('qr.svg', code, (er)=> console.log(er));
// and download using express.static
res.type('svg');
code.pipe(res);
};
目前我正在以流的形式返回图像,它工作正常。
我有一个 api 和 mongodb 构建在 Express 上,我想在服务器端存储 QR 码。 Api 适用于使用 Xamarin 管理事件门票构建的应用程序。
QR 图像将被下载不止一次,这就是为什么我想将它们放在服务器上。
也许更好的方法是将它们与 SQLite 本地存储在客户端设备上?或者我应该只发送 json 数据以解析为 SVG?
你怎么看?
目前我没有在本地实现数据库。
一段时间后,当我回到主题时,我找到了解决方案。
您需要向管道添加新操作。 就这么简单:
var code = qr.image(ticket.code, { type: 'svg' });
code.pipe(fs.createWriteStream('i_love_qr.svg'));
我是 Express 的新手,我需要你的帮助。
如何使用 Express 在服务器上保存 SVG?
const qr = require('qr-image');
const fs = require('fs');
exports.qr = function (req, res) {
var code = qr.image(new Date().toString(), { type: 'svg' });
// I would like to do something like this:
fs.writeFile('qr.svg', code, (er)=> console.log(er));
// and download using express.static
res.type('svg');
code.pipe(res);
};
目前我正在以流的形式返回图像,它工作正常。
我有一个 api 和 mongodb 构建在 Express 上,我想在服务器端存储 QR 码。 Api 适用于使用 Xamarin 管理事件门票构建的应用程序。
QR 图像将被下载不止一次,这就是为什么我想将它们放在服务器上。
也许更好的方法是将它们与 SQLite 本地存储在客户端设备上?或者我应该只发送 json 数据以解析为 SVG?
你怎么看?
目前我没有在本地实现数据库。
一段时间后,当我回到主题时,我找到了解决方案。 您需要向管道添加新操作。 就这么简单:
var code = qr.image(ticket.code, { type: 'svg' });
code.pipe(fs.createWriteStream('i_love_qr.svg'));