Streaming static file server based on LevelDB.
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.handle.bind(server)).listen(8000);
console.log('go to http://localhost:8000' + server.url('cat.png'));
ETags are used to save bandwidth and reduce load.
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.
This concept involves two instances of level-serve
.
- One is read only and serves the whole database over http.
- The other one is given to clients / plugins and they can write whatever they want, however only see their own section of the db, their sublevel.
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' + dbServer.url('white.png'));
/files/ (sublevels /) file-id
Generate URLs using Server#url
.
Make sure that db
has been opened with valueEncoding: 'binary'
if you want
to serve binary files.
HTTP request handler. Pass this to http.createServer()
or express
for
example.
If next
is passed it will be called when errors happen or a requested file
cannot be found.
Store a file under file-id
. If you give file-id
an extension it will be
served with the correct mime type.
Retrieve a file stored under file-id
.
Store data
as file-id
. Convenience method that exposes the write stream
in an async api.
Get the url of file-id
, respecting sublevels.
With npm do
$ npm install level-serve
(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.