filed

简化文件库。「Simplified file library.」

  • 所有者: mikeal/filed
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

filed -- Simplified file library.

Install

Super simple to use

Filed does a lazy stat call so you can actually open a file and begin writing to it and if the file isn't there it will just be created.

var filed = require('filed');
var f = filed('/newfile')
f.write('test')
f.end()

Streaming

The returned file object is a stream so you can do standard stream stuff to it. Based on what you do the object it will be a read stream, a write stream.

So if you send data to it, it'll be a write stream.

fs.createReadStream.pipe(filed('/newfile'))

If you pipe it to a destination it'll be a read stream.

filed('/myfile').pipe(fs.createWriteStream('/out'))

And of course you can pipe a filed object from itself to itself and it'll figure it out.

filed('/myfile').pipe(filed('/newfile'))

Those familiar with request will be familiar seeing object capability detection when doing HTTP. filed does this as well.

http.createServer(function (req, resp) {
  filed('/data.json').pipe(resp)
})

Not only does the JSON file get streamed to the HTTP Response it will include an Etag, Last-Modified, Content-Length, and a Content-Type header based on the filed extension.

http.createServer(function (req, resp) {
  req.pipe(filed('/newfile')).pipe(resp)
})

When accepting a PUT request data will be streamed to the file and a 201 status will be sent on the HTTP Response when the upload is finished.

During a GET request a 404 Response will be sent if the file does not exist.

http.createServer(function (req, resp) {
  req.pipe(filed('/data.json')).pipe(resp)
})

The Etag and Last-Modified headers filed creates are based solely on the stat() call so if you pipe a request to an existing file the cache control headers will be taken into account; a 304 response will be sent if the cache control headers match a new stat() call. This can be very helpful in avoiding unnecessary disc reads.

http.createServer(function (req, resp) {
  req.pipe(filed('/directory')).pipe(resp)
})

Just to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.

License

ASL 2.0, see LICENSE file.

主要指標

概覽
名稱與所有者mikeal/filed
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台
許可證Apache License 2.0
所有者活动
創建於2011-10-28 03:10:24
推送於2019-11-06 18:30:04
最后一次提交2019-04-13 02:26:15
發布數2
最新版本名稱v0.1.1 (發布於 2013-05-13 14:31:23)
第一版名稱v0.1.0 (發布於 2013-05-13 14:31:07)
用户参与
星數279
關注者數14
派生數24
提交數49
已啟用問題?
問題數18
打開的問題數14
拉請求數14
打開的拉請求數4
關閉的拉請求數3
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?