Package Exports
- @sifrr/server
- @sifrr/server/src/server/sendfile
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sifrr/server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
sifrr-server · 
NodeJS Server based on uWebSocket.js with extended API to create static/api server.
Features
- Extends uWebSocket.js
- Simple static file serving
- Simple post request data and form data handling
How to use
Do npm i @sifrr/server or yarn add @sifrr/server or add the package to your package.json file.
Api
Basic usage
Sifrr Server extends 'uWebSockets.js' package. You can view more details here. So all the APIs from uWS works with sifrr server.
const { App, SSLApp } = require('@sifrr/server');Appextends uWS.AppSSLAppextends uWS.SSLApp
Extra APIs than uWS
writeHeaders
const { App, writeHeaders } = require('@sifrr/server');
const app = new App();
app.get('/', res => {
writeHeaders(res, name, value); // single header
writeHeaders(res, {
name1: value1,
name2: value2
}); // multiple headers
});sendFile
respond with file from filepath. sets content-type based on file name extensions, supports responding 304 based on if-modified-since headers, compression(gzip, brotli, deflate), range requests (videos, music etc.)
const { sendFile } = require('@sifrr/server');
const app = new App();
app.get(uWSRoutingPattern, res => {
sendFile(res, filepath, options)
});options:lastModified: default:trueresponds with304 Not Modifiedfor non-modified files if this is set to trueheaders: default:{}Additional headers to set on response ,compress: default:trueresponses are compressed if this is set to true and ifaccept-encodingheader has supported compressions (gzip, brotli, deflate)compressionOptionsdefault:{ priority: [ 'gzip', 'br', 'deflate' ] }which compression to use in priority
host static files
- Single file (alias for sendFile example above)
file from filepath will be server for given pattern
app.file(uWSRoutingPattern, filepath, options); // options are sendFile options- Folder
Whole folder will be server recursively under giver prefix
app.folder(prefix, folder, options); // options are sendFile options
// Example
// if you have a file named `example.html` in folder `folder`, then doing this
app.folder('/example', folder);
// will serve example.html if you go to `/example/example.html`Post requests
for post responses there are extra helper methods added to uWS response object (res is a response object given by Sifrr Server on post requests):
res.body().then(body => /* do something */): gives post body as bufferres.bodyStream(): Gives post body streamres.json().then(jsonBody => /* do something */): gives post body as json if content-type isapplication/json(this method is only set if post body content-type isapplication/json)res.formData().then(data => /* do something */)(only set if content-type isapplication/x-www-form-urlencodedormultipart/form-data)
res.formData(options).then(data => {
// example data
// {
// file: {
// filename: 'name.ext',
// encoding: '7bit',
// mimetype: 'application/json',
// filePath: 'tmpDir/name.ext' // only set if tmpDir is given
// },
// fieldname: value
// }
})options need to have atleast one of onFile function or tmpDir if body has files else request will timeout and formData() will never resolve.
- if
onFileis set, then it will be called withfieldname, file, filename, encoding, mimetypefor every file uploaded, where file is file stream, you need to consume it or the request will never resolve - if
tmpDiris given (folder name), files uploaded will be saved in tmpDir, and filePath will added in given data onField(optional): will be called withfieldname, valueif given
Examples
Are available in test/public/benchmarks/sifrr.js
graphql server
app.post('/graphql', res => {
res.onAborted(err => { throw err; });
res.json().then(({ query, variables }) => {
res.end(JSON.stringify(graphql({
schema: executableSchema,
source: query,
variableValues: variables,
context: { /* set context */ }
})));
});
});