Fast Stream HTTP Server
$ npm install fast-stream
Simple server configuration conf
, serve all requests with 200
OK.
const http = require('fast-stream');
const conf = {
'*': { // host name "*" <for all>, "cb" is the callback function
404: cb => cb('<html><body><h3>Hello World!</h3></body></html>', null, 200)
}
};
require('net').createServer( // or require('tls') for HTTPS / SSL
socket => socket.pipe(new http(conf)).pipe(socket)
).listen(80); // or 443 for HTTPS / SSL
Create conf
using fast-config module for static files.
const get = require('fast-config');
const http = require('fast-stream');
const conf = {
'*': get('/path/src') // static files directory
};
require('net').createServer(socket => socket.pipe(new http(conf)).pipe(socket)).listen(80);
Sample conf
for files or readable streams, mimehttp optional.
const fs = require('fs');
const mime = require('mimehttp');
const conf = {
'*': {
GET: { // method GET
'/favicon.ico': cb => cb({
src: '/dir/favicon.ico' // source: file path
}, { // additional header
'Content-Type': mime.type.ico
}),
'/vid.mp4': cb => cb({
src: fs.createReadStream('/dir/vid.mp4') // source: readable Stream
}, { // additional headers
'Content-Type': mime.type['mp4'],
'Content-Disposition': 'inline', // display in browser
'Content-Duration': 171, // required for web video player
'X-Content-Duration': 171 // video duration in seconds
})
}
}
};
Function host
arguments cb
, req
and this
bind example.
const conf = {
'localhost:80': { // hostname "localhost" port "80"
GET: { // URL: http://localhost/
'/': cb => cb('<html><body>' + // attach small files, or remove JSON.stringify(req), see below
'<form action="/attach.html" method="post" enctype="multipart/form-data">' +
'<input type="text" name="t1"><input type="text" name="t2"><input type="text" name="t2">' +
'<input type="file" name="f1"><input type="file" name="f2"><input type="file" name="f2">' +
'<input type="submit" value="Submit">' +
'</form>' +
'</body></html>')
},
POST: { // URL: http://localhost/attach.html (method POST only)
'/attach.html': function host(cb, req) {
cb('<html><body>' + // client IP address
'<h3>' + this._readableState.pipes.remoteAddress + '</h3>' +
'<code>' + JSON.stringify(req) + '</code>' +
'</body></html>'); // default 'Content-Type' is 'text/html' utf8
}
}
},
'127.0.0.1:80': { // another host
GET: { // URL: http://127.0.0.1/
'/': cb => cb('Request from 127.0.0.1:80', { 'Content-Type': 'text/plain' })
}
}
};
config
Object - host functions list, see the examples aboveoptions
Object - see below
cb
Function - callback function, see belowreq
Object - request, see belowthis
Bind Object - this Stream
data
String|Buffer|Object - response, forObject
see belowheaders
Object - optional, default nullcode
Number - optional, http status, default 200
src
String|Object -String
file path orObject
readable streamlength
Number - optional, data size, required for range bytes whensrc
is readable stream
path
Stringquery
Object - headerquerystring
host
Stringhostname
Stringport
Numberattach
Object - whenreq.request.method
isPOST
, see belowrequest
Object - {method
: String,uri
: String,protocol
: String }header
Object - {list
: Array,hostname
: String,port
: Number,length
: Number,connection
: String,type
: String,boundary
: String,etag
: String,modified
: String,range
: String }
- when
req.header.type
isurlencoded
- Objectquerystring
fromPOST
body - when
req.header.type
ismultipart
- Object { query: Objectquerystring
, files: Array Object { name: String, data: Buffer } }
limit
Number - anti memory overhead, request data maximum size, default5e8
~500MB, for big data/files, consider to increase this valueranges
Boolean - accept ranges request, defaulttrue
error
String - custom error name event, defaulthttpError
name
String - Server name/version, defaultfast-stream/2.2
,null
- to disablecache
Boolean - client cache, send/verify "Last-Modified" and/or "ETag" header, defaulttrue
closeOnError
Boolean - close connection on statuscode
>=400
, defaultfalse
, don't closechunked
Number - if body response size is greater than this value, send "Transfer-Encoding: chunked", default2e7
~20MB,0
- to disable
Fast Stream is licensed under the MIT license. See the included LICENSE
file for more details.