Home > Software design >  How can i create createReadStream of a video located in a different server from that where we host t
How can i create createReadStream of a video located in a different server from that where we host t

Time:10-07

I have a nestjs project in which I need to store the videos and save their paths in the mysql database my question is: how can I create a createReadStream of one of these videos knowing that the videos and the database will be in one server and the application in another server ??

CodePudding user response:

fs.createReadStream() works just fine if you give it a full OS path. If you're having trouble with that, then we need to see what your "absolute path" is.

Usually, you would not store a full OS path in a database because that "hard wires" your implementation to a specific disk layout. Instead, you would usually store a relative path only in the database and then combine that with a base path that's part of the configuration of your app. That gives you more flexibility if you ever need to rearrange how things are stored on disk without having to rewrite every path in the database. Instead, you can just change the base path in your configuration to point to the new path.

For example, imagine you outgrow your current system disk and add a new faster and larger disk. When storing only relative paths in the database, you could shut-down your app, copy all the files over to the new disk, then adjust the base path in your app, restart your app and it would be up and running with the new location. If you had stored the absolute path in your database, you'd have to write a DB script tor rewrite every single path in the database to the new location.

can i host the database in one server and the application in anthor ? if yes how can i do that ??

The database can be wherever you want. But, the files themselves that you want to stream as a response to an http request will have to be accessible from the web server. If you want to use fs.createReadStream() as the source to stream them, then the files have to be accessible via an OS file path because fs.createReadStream() only works with a file path. If the files are stored elsewhere that doesn't have OS file path access to your web server (like say in a cloud service), you'd have to find some other way to read/stream them from your web server.

How can I create a createReadStream of one of these videos knowing that the videos and the database will be in one server and the application in another server

You can use fs.createReadStream() only if you have OS level file access to the other server (likely via some shared file system mechanism). For example, the files could be stored on some shared file server.

If you don't have OS level file access, then you will have to assess what type of access you do have and pick an appropriate mechanism for streaming the files from there. For example, if you have web access to the files (they are accessible via some URL), then you could either redirect the client to get the files directly from the other web server or you could stream them from that other server yourself using an http library that supports streaming such as got() or axios(). You could then pipe that http stream into your response - similar to what you would do with the stream from fs.createReadStream().

  • Related