Git Product home page Git Product logo

filed's Introduction

filed -- Simplified file library.

Install

  npm install filed

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.

filed's People

Contributors

brianc avatar indexzero avatar jed avatar kesla avatar max-mapper avatar mikeal avatar naholyr avatar pnemade avatar trentm avatar tshinnic avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.