简体   繁体   中英

What is the most efficient way to send images from server to client?

I have created a node.js server and a website with express. The site is mainly to show loads of pictures that are related such as photos taken in 2017. Server is running on a Raspberry pi with an external HDD connected to it which the server takes images from. When the user accesses the site, the client sends an initiation request to server via websockets to start sending images. The server then sends X images also via websockets and the user can request more with another initiation request. How can I make this more efficient? Is there a faster way than websockets?

A few remarks

  1. I know the Pi is inherently slow and that the HDD is a bottleneck. Yet, I want to optimise the data transfer.
  2. I have already looked on stack overflow in another question and it recommended to strip meta-data which I have done.

Server-side code:

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");
  }

Client-side just receives the image, puts it on the site and then sends back a request for one more image until it reaches X images.

hope this helps

const path = require("path");
const fs = require('fs');

exports.getBookImage = async (req, res) => {
    let filepath = path.join(__dirname + `/root/folder/file`);
    res.sendFile(filepath);
};

here __dirname is an inbuilt function to get path of current file getting executed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM