In the Node.js, there are several ways to stream a file from back-end to front-end:
- Creating a HTTP server in
Node.jsand using thefsmodule to read the file and write it to the HTTP response. - Streams API
- Using a third-party libraries such as FileStream, Multer, or Formidable
Creating a HTTP server is more straightforward and better to understand. However, The Streams API provides more flexibility. The front-end will be created later to show the file sent to it by streaming. The front-end needs to communicate with the back-end again and send the changed parts to the back-end. Streams API can act with more flexibility in this case and it supports different types of streams. And finally, third parties are not considered in this project.
To test the stream manually, use following command in terminal (in windows, use a bash):
curl -o output.txt http://localhost:3000SOLID principle has been considered during the design of this project (backend):
-
Single responsibility Each file does a specific work. Router is for routing only for example.
-
Open Close To add new features, do not modify new ones, instead add new files.
-
LiksovandInterfaceare more about pure object oriented features. -
Dependency Injectis for heavy service based solutions using interfaces
- Add unit tests
- Consider file upload and download when the file is too big. They need to be streamed.
- Sanitize file name of uploaded file to prevent access to parent directory for example (upload API)
- Be aware of file content. for example do not allow uploading js or python codes
- Guard against common XSS and other script attacks