2.2.2 • Published 9 years ago
fast-stream v2.2.2
Fast Stream
Fast Stream HTTP Server
$ npm install fast-streamSimple 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 / SSLCreate 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' })
}
}
};http (config, options) class
configObject - host functions list, see the examples aboveoptionsObject - see below
host (cb, req) host function
cbFunction - callback function, see belowreqObject - request, see belowthisBind Object - this Stream
cb (data, headers, code) callback function
dataString|Buffer|Object - response, forObjectsee belowheadersObject - optional, default nullcodeNumber - optional, http status, default 200
data Object response
srcString|Object -Stringfile path orObjectreadable streamlengthNumber - optional, data size, required for range bytes whensrcis readable stream
req Object request
pathStringqueryObject - headerquerystringhostStringhostnameStringportNumberattachObject - whenreq.request.methodisPOST, see belowrequestObject - {method: String,uri: String,protocol: String }headerObject - {list: Array,hostname: String,port: Number,length: Number,connection: String,type: String,boundary: String,etag: String,modified: String,range: String }
req.attach Object attach
- when
req.header.typeisurlencoded- ObjectquerystringfromPOSTbody - when
req.header.typeismultipart- Object { query: Objectquerystring, files: Array Object { name: String, data: Buffer } }
options Object http class argument
limitNumber - anti memory overhead, request data maximum size, default5e8~500MB, for big data/files, consider to increase this valuerangesBoolean - accept ranges request, defaulttrueerrorString - custom error name event, defaulthttpErrornameString - Server name/version, defaultfast-stream/2.2,null- to disablecacheBoolean - client cache, send/verify "Last-Modified" and/or "ETag" header, defaulttruecloseOnErrorBoolean - close connection on statuscode>=400, defaultfalse, don't closechunkedNumber - 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.
2.2.2
9 years ago
2.2.1
9 years ago
2.2.0
9 years ago
2.1.2
9 years ago
2.1.1
9 years ago
2.1.0
9 years ago
2.0.2
9 years ago
2.0.1
9 years ago
2.0.0
9 years ago
1.2.3
9 years ago
1.2.2
9 years ago
1.2.1
9 years ago
1.2.0
9 years ago
1.1.7
9 years ago
1.1.6
9 years ago
1.1.5
9 years ago
1.1.4
9 years ago
1.1.3
9 years ago
1.1.2
9 years ago
1.1.1
9 years ago
1.1.0
9 years ago
1.0.6
9 years ago
1.0.5
9 years ago
1.0.4
9 years ago
1.0.3
9 years ago
1.0.2
9 years ago
1.0.1
9 years ago
1.0.0
9 years ago
