将图像从服务器发送到客户端的最有效方法是什么?
我创建了一个node.js服务器和一个带快递的网站。该网站主要是显示相关的图片,如2017年拍摄的照片。服务器在Raspberry pi上运行,外接硬盘连接到服务器,服务器从中获取图像。当用户访问站点时,客户端通过websockets向服务器发送启动请求以开始发送图像。然后,服务器也通过websockets发送X图像,用户可以使用另一个启动请求请求更多。我怎样才能提高效率呢?有没有比websockets更快的方法?
一些评论
- 我知道Pi天生就很慢,硬盘是一个瓶颈。但是,我想优化数据传输。
- 我已经在另一个问题中查看了堆栈溢出,它建议去除我已经完成的元数据。
服务器端代码:
con.on("message", function incoming(message) {
let msgArr = message.split(", ");
let prefix = 'a';
let year = '2008';
if (msgArr[0].includes('2008')) {prefix = 'a'; year = '2008'}
if (msgArr[0].includes('2009')) {prefix = 'b'; year = '2009'}
if (msgArr[0].includes('2010')) {prefix = 'c'; year = '2010'}
if (msgArr[0].includes('2011')) {prefix = 'd'; year = '2011'}
if (msgArr[0].includes('2012')) {prefix = 'e'; year = '2012'}
if (msgArr[0].includes('2013')) {prefix = 'f'; year = '2013'}
if (msgArr[0].includes('2014')) {prefix = 'g'; year = '2014'}
if (msgArr[0].includes('2015')) {prefix = 'h'; year = '2015'; msgArr[1]++;}
if (msgArr[0].includes('2016')) {prefix = 'i'; year = '2016'; }
if (msgArr[0].includes('2018')) {prefix = 'j'; year = '2018';}
let path = "/Volumes/Seagate\ Drive/" + year + "/" + prefix + msgArr[1] + ".jpg";
fs.readFile(path, function (err, data) {
if (err) {con.send("error")}
else {
con.send(data.toString('base64'));
console.log("image sent");
}
客户端只接收图像,将其放在网站上,然后再发送一个图像请求,直到它到达X图像。
回答如下:希望这可以帮助
const path = require("path");
const fs = require('fs');
exports.getBookImage = async (req, res) => {
let filepath = path.join(__dirname + `/root/folder/file`);
res.sendFile(filepath);
};
这里__dirname是一个内置函数,用于获取当前文件的执行路径
将图像从服务器发送到客户端的最有效方法是什么?
我创建了一个node.js服务器和一个带快递的网站。该网站主要是显示相关的图片,如2017年拍摄的照片。服务器在Raspberry pi上运行,外接硬盘连接到服务器,服务器从中获取图像。当用户访问站点时,客户端通过websockets向服务器发送启动请求以开始发送图像。然后,服务器也通过websockets发送X图像,用户可以使用另一个启动请求请求更多。我怎样才能提高效率呢?有没有比websockets更快的方法?
一些评论
- 我知道Pi天生就很慢,硬盘是一个瓶颈。但是,我想优化数据传输。
- 我已经在另一个问题中查看了堆栈溢出,它建议去除我已经完成的元数据。
服务器端代码:
con.on("message", function incoming(message) {
let msgArr = message.split(", ");
let prefix = 'a';
let year = '2008';
if (msgArr[0].includes('2008')) {prefix = 'a'; year = '2008'}
if (msgArr[0].includes('2009')) {prefix = 'b'; year = '2009'}
if (msgArr[0].includes('2010')) {prefix = 'c'; year = '2010'}
if (msgArr[0].includes('2011')) {prefix = 'd'; year = '2011'}
if (msgArr[0].includes('2012')) {prefix = 'e'; year = '2012'}
if (msgArr[0].includes('2013')) {prefix = 'f'; year = '2013'}
if (msgArr[0].includes('2014')) {prefix = 'g'; year = '2014'}
if (msgArr[0].includes('2015')) {prefix = 'h'; year = '2015'; msgArr[1]++;}
if (msgArr[0].includes('2016')) {prefix = 'i'; year = '2016'; }
if (msgArr[0].includes('2018')) {prefix = 'j'; year = '2018';}
let path = "/Volumes/Seagate\ Drive/" + year + "/" + prefix + msgArr[1] + ".jpg";
fs.readFile(path, function (err, data) {
if (err) {con.send("error")}
else {
con.send(data.toString('base64'));
console.log("image sent");
}
客户端只接收图像,将其放在网站上,然后再发送一个图像请求,直到它到达X图像。
回答如下:希望这可以帮助
const path = require("path");
const fs = require('fs');
exports.getBookImage = async (req, res) => {
let filepath = path.join(__dirname + `/root/folder/file`);
res.sendFile(filepath);
};
这里__dirname是一个内置函数,用于获取当前文件的执行路径