nodejs - send multiple files to the client - node.js

I need to create nodejs http server and the Angular client.
The server listen to the incoming http request in which clients specify the query parameter
/api/pictures?query=some query
Let’s say I have a folder ‘pictures’ that contains:
pic1.jpg
pic2.jpg
pic3.jpg
instruction.jpg
manual Instruction.jpg
…
Whenever the client sends the request with url like:
/api/pictures?query=pic
The server should returns files whose names contains the query specified by the client. In this case:
pic1.jpg
pic2.jpg
pic3.jpg
And if the client sends the request with url like:
/api/pictures?query=instruction
The server should returns:
instruction.jpg
manual Instruction.jpg
My question is, how to send these files in the most efficient way?
I was thinking about streaming these files, but it’s rather impossible for the browser client to read the files from such a stream
Or maybe just read all the pictures that matches the criteria to the memory, zip them and then send them.
But I believe there is efficient way to do that, any idea guys? :)

Related

Send updates about the state of a running HTTP request to the client

I would like to send updates of a running HTTP request to the client to tell it at what stage the request-triggered process currently is.
The process behind the request does currently the following things (in this order):
Client-side:
Client sends an HTTP Request (upload of a file) to the server
Server-side:
Takes the uploaded file
Encrypt it
Upload it to an archive storage
Return response to the client
(Meanwhile, the client does not know what currently happens)
Client-side:
Get response and show it to the user
I want to tell the client at what stage the process is, like “Uploading done. Encrypting…” and so on.
Is there a way to realize that, or am I missing something? Is it even possible to do?
Frameworks I'm using:
Client: Next.js
Server: Hapi.dev for API development
Thanks
You can send non-final 1xx header responses for your http request as described here.

Clustered HTTP server that syncs clients for file transfer

I'm writing a Node HTTP server that essentially only exists for NAT punchthrough. Its job is to facilitate a client sending a file, and another client receiving that file.
Edit: The clients are other Node processes, not browsers. We're using Websockets because some client locations won't allow non-HTTP/S port connections.
The overall process works like this:
All clients keep an open websocket connection.
The receiving client (Alice) tells the server via Websocket that it wants a file from another client (Bob).
The server generates a unique token for this transaction.
The server notifies Alice that it should download the file from /downloads?token=xxxx. Alice connects, and the connection is left open.
The server notifies Bob that it should upload the file to /uploads?token=xxxx. Bob connects and begins uploading the file, since Alice is already listening on the other side.
Once the transfer is complete, both connections are closed.
This is all accomplished by storing references to the HTTP req and res objects inside of a transfers object, indexed by the token. It all works great... as long as I'm not clustering the server.
In the past, when I've used clustering, I've converted the server to be stateless. However, that's not going to work here: I need the req and res objects to be stored in a state so that I can pipe the data.
I know that I could just buffer the transferring data to disk, but I would rather avoid that if at all possible: part of the requirements is that if I buffer anything to disk I must encrypt it, and I'd rather avoid putting the additional load of encrypting/decrypting transient data on the server.
Does anyone have any suggestion on how I could implement this in a way that supports clustering, or a pointer to further research sources I could look at?
Thanks so much!

Server Raw HTTP response from file

I have two files on disk. One which contains the exact HTTP headers I want to respond with and one which contains the raw HTTP body that I want to return.
What's the easiest way to have node return these two files so that it looks like a normal HTTP response to the caller.
I don't want to parse the file and then call res.writeHead() or something like that. I just want to tell node, here's the exact data I want you to push back to the user.
Use a plain TCP server instead of an HTTP server? If you need to parse the request still, you could use node's HTTP parser binding directly (see http.js in node core).

can I have different websockets input in the same server - output in different clients

Let me clarify the title of the question.
On the client side I have two different html files, client1.html and client2.html. They send data via websockets to the same server.js file, in node.js.
On the server side, if the data came from client1.html I want to perform a query and send the outcome to the client1.html. On the same server, if the data came from client2.html I want to perform another query and send to the client2.html the message "Data Saved".
I guess, I have to create two different functios in the server.js. OK.
But, my problem is, on the server side, how to tell, which data came from which client?
Also, how the server can send back the right message to the right client?
Thanks in advance
You have to register your clients. For example if a user A is on page client1.html then you send a message (via websockets) for example JSON (or any other format you like):
{ "user": "A", "page": "client1.html" }
Now on the server side you just mark that this user/connection came from client1.html. You can add for example a custom property:
conn.source = "client1.html";
Or any other way (depending for example on framework).
You might even use a handshake for this (instead of sending JSON): when connecting to the server do for example (on the client side):
var ws = new WebSocket("ws://myserver/client1.html");
Now you just have to do the same in handshake code (client1.html is a part of URL now in handshake).
As for other question: on the server side you keep lists of all users for client1.html, client2.html, etc. The rest is obvious: you loop over a target list and send a notification to those users.
Of course there are many small details here. Like you have to remove users from lists if a connection is dead (thus you need a background task to check whether a connection is alive), etc. But that's the general idea.

connecting two client socket

i have an application that has client and a server. The server is basically only used to store the file names that the clients have so that when other clients want to search for files, they can go the server, find the client that has the file they want and receive the file by directly connecting to it. By now, i can get the socket information of the client that has the file requested by the other client. However, i am now confused about how to connect these two clients. Do i have to create a separate client and a server socket between the two clients or there are other ways.
Now You have two choices:-
Let the server continue his role, and the server can act as an intermediary between the two parties. It should download the file from the client which has it and send it (via any suitable protocol) to the client who requested the file. This is called the Client -Server Architecture. This is a simple approach and you have the benefits such as file caching etc. i.e. If in future same file is requested the server can send it directly without asking for the client.
You can continue using the P2P architecture, and Create a separate a socket between the two parties, this is not straight forward and needs special care when multiple processes are working simultaneously.

Resources