sierra
v0.6.0-rc1.5
Published
A service framework for Node.js.
Downloads
28
Maintainers
Readme
Sierra
Middleware for your Node.js Server Application.
Sierra provides Promise based Middleware and HTTP Request handling.
Create a Handler
Sierra uses a Middleware pipeline to process HTTP Requests. To get up and running, create a new Handler
instance.
import { createHandler } from 'sierra';
const handler = new createHandler();
Add Middleware
Middleware
can be added to the Handler
.
handler.use((context, value) => {
return 'output';
});
Handler.use
calls can be chained together. This is especially useful for strong typing.
interface BodyContext {
body: {
value: string;
}
}
interface SessionContext {
session: {
user: string;
}
}
const handler = createHandler()
.use<BodyContext>(({ data }) => {
data.body = {
value: 'test',
};
})
.use<SessionContext>(({ data }) => {
data.session = {
user: 'admin',
};
})
.use(({ data }) => {
const { body, session } = data;
const { value } = body;
const { user } = session;
return {
value,
user,
};
});
Create a Server
A Handler.callback
function may be passed to a Node.js http.Server
instance.
import { createServer } from 'http';
import { createHandler } from 'sierra';
const handler = createHandler();
const server = createServer(handler.callback);
server.listen(80);
A special Promise
based PromiseServer
is also provided.
When using
createServer
from Sierra, you may passHandler
instances directly.
import { createHandler, createServer } from 'sierra';
const handler = createHandler();
const server = createServer(handler);
server.start(80);