expressjs send file

在 expressjs傳送檔案可以直接使用res.sendFile或是開啟readable stream再pipe到res

fs.createReadStream(fpath).pipe(res)

但在某些情況(例如video播放)這樣的方式太陽春,沒辦法seek (http byte range request)

特別是例如iOS safari browser在播放mp4影片時,會要求伺服器支援byte range request,不然不會播放。
Safari Web Content Guide#Configuring Your Server

HTTP servers hosting media files for iOS must support byte-range requests, which iOS uses to perform random access in media playback. (Byte-range support is also known as content-range or partial-range support.)

事實上,nodejs有一個send module(被serve-static引用)可以很方便處理這樣的情況。

send(req, fpath).pipe(res)

就能讓檔案傳送支援range request了!

This entry was posted in nodejs. Bookmark the permalink.

Leave a Reply