1.5.0 • Published 11 years ago

level-serve v1.5.0

Weekly downloads
3
License
-
Repository
github
Last release
11 years ago

level-serve

Streaming static file server based on LevelDB.

Usage

Store and serve a nice cat picture at /files/cat.png.

var Server = require('level-serve');
var level = require('level');
var http = require('http');
var fs = require('fs');

// initialize db and server
var db = level(__dirname + '/.server-db', { valueEncoding: 'binary' });
var server = Server(db);

// store cat
var ws = server.createWriteStream('cat.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);

// serve cat
http.createServer(server.serve.bind(server)).listen(8000);
console.log('go to http://localhost:8000' + server.url('cat.png'));

Sublevels

With sublevels you can e.g. give a plugin or a user only access to a part of the database so they can't do anything harmful. The location in the sublevel tree is reflected in the resulting public url.

var Server = require('level-serve');
var level = require('level');
var http = require('http');
var SubLevel = require('level-sublevel');
var fs = require('fs');

// initialize the db and give it sublevels
var db = level(__dirname + '/.sublevel-db', { valueEncoding: 'binary' });
SubLevel(db);

// initialize server for sublevel
var server = Server(db.sublevel('cool').sublevel('cats'));

// cat from sublevel "cool"."cats" will be served at /files/cool/cats/white.png
var ws = server.createWriteStream('white.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);

// serve cat
var dbServer = Server(db);
http.createServer(dbServer.serve.bind(dbServer)).listen(8000);
console.log('or to http://localhost:8000' + server.url('white.png'));

URLs

/files/ (sublevels /) file-id

Generate URLs using Server#url.

API

Server(db)

Make sure that db has been opened with valueEncoding: 'binary' if you want to serve binary files.

Server#serve(req, res, next)

HTTP request handler. Pass this to http.createServer() or express for example.

Server#createWriteStream(file-id)

Store a file under file-id. If you give file-id an extension it will be served with the correct mime type.

Server#write(file-id, data, cb)

Store data as file-id. Convenience method that exposes the write stream in a async api.

Server#url(file-id)

Get the url of file-id, respecting sublevels.

Installation

With npm do

$ npm install level-serve

License

(MIT)

Copyright (c) 2013 Wayla <data@wayla.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.5.0

11 years ago

1.4.0

11 years ago

1.3.1

11 years ago

1.3.0

11 years ago

1.2.0

11 years ago

1.1.1

11 years ago

1.1.0

11 years ago

1.0.0

11 years ago

0.0.0

11 years ago