Full-featured slim webserver for microservices with extremely low overhead and ready for production
Node.js >= 14.8
Create directory
mkdir backend-application
cd backend-application
Install the service
npm
npm i barehttp --save
yarn
yarn add barehttp
import { BareHttp, logMe } from 'barehttp';
const app = new BareHttp();
app.route.get({
route: '/route',
handler: function routeGet(flow) {
flow.json({ everything: 'OK' });
})
});
// you can chain the routes
app.route
.post({
route: '/route',
handler: async function routePost(flow) {
return 'RESPONSE POST';
},
})
.route.patch({
route: '/route',
handler: async function routePatch(flow) {
return 'RESPONSE PATCH';
})
// Define a middleware
app.use((flow) => {
logMe.info('my middleware');
});
app.start((address) => {
console.log(`BareServer started at ${address}`);
});
import { BareHttp, logMe } from 'barehttp';
const app = new BareHttp({ logging: true });
app.route.get({
route:'/route',
handler: async function routeV1() {
return { promised: 'data' };
})
});
// Define a middleware
app.use(async (flow) => {
logMe.info('my middleware');
await someAuthenticationFlow();
});
app.start((address) => {
console.log(`BareServer started at ${address}`);
});
An instance of the application
Options submitted to the server at initialization
If provided, this will apply an array of middlewares to each incoming request. The order of the array is the order of middlewares to apply
Default 3000
Listening port
Default '0.0.0.0'
Address to bind the web server to
Default false
Enables request context storage accessible through all application importing import { context } from 'barehttp';
Default false
Enable request/response logging, format varies from production
or development
environments, though to change use e.g. NODE_ENV=production
Default false
Enable WebSocket support through ws
package.
Refer to ws documentation.
If provided, will set a custom error handler to catch the bubbled errors from the handlers
Default 's'
- seconds
Request execution time format in seconds
or milliseconds
Control over cookies. If enabled this will turn on automatic cookies decoding, if you want to set up more settings for this (e.g. signed cookies), next option is available
To set options for the cookies decoding/encoding
If enabled, and also with logging: true
, will try to log the resolved reverse DNS of the first hop for remote ip of the client (first proxy).
Logs follow an Apache Common Log Format
Default false
Exposes a basic report with the routes usage under GET /_report
route
Attach a middleware after
the middlewares optional array.
The order of the middlewares is followed by code declarations order.
To set a route for get | post | patch | put | delete | options | head
with following parameters:
route
(String) - should follow a format of/your_route
, including params as/your_route/:param
options
(Object) -RouteOptions
handler
(Function) - A function with the signature(flow: BareRequest) => Promise<any> | any
method
(Array) - if the method isdeclare
you have to indicate an array of methods to declare the route e.g.['get', 'post']
Example
app.route.get({
route: '/route',
options: { timeout: 2000 },
handler: async (flow) => {
return 'My route response';
},
});
app.route.declare({
route: '/declared_route',
handler: () => {
return 'My declared route response';
},
methods: ['post', 'patch'],
});
Same as the above routes API, but you can only declare them when the server is listening
app.runtimeRoute
.get({
route: '/route',
options: { timeout: 2000 },
handler: async (flow) => {
return 'My route response';
},
})
.declare({
route: '/declared_runtime_route',
handler: () => {
return 'My declared runtime route response';
},
methods: ['post', 'patch'],
});
If set, provide per-route options for behavior handling
Disables all cache headers for the response. This overrides any other cache setting.
If set, provides a granular cache headers handling per route.
Request timeout value in ms
. This will cancel the request only for this route if time expired
Based on ws
package, for internals please refer to external WebSocketServer for documentation.
This particular implementation works out easily for WebSockets interaction for pushing data to server from the clients and waiting for some answer in async.
Also exposes an way to keep pushing messages to the Client from the Server on server handle through internal clients list. (WIP optimizing this)
This is the main 'handler' function for any kind of Client request. If there's a response to that push from the client the return should contain it, otherwise if the response is void
there will be no answer to the client side.
Data
: is the data received from the client for this exactType
UserClient
: is an optional client defined on the stage ofUpgrade
to provide some closured client data to be able to know what Client is exactly making the request to the ServerWSClient
: raw instance ofws.Client & { userClient: UC }
MessageEvent
: raw instance ofws.MessageClient
Code Example:
app.ws?.declareReceiver<{ ok: string }>({
type: 'BASE_TYPE',
handler: async (data, client) => {
// do your async or sync operations here
// return the response if you need to send an answer
return { cool: 'some answer', client };
},
});
To de able to handle authorization or any other previous operation before opening and upgrading an incoming client's request. If this function is not initialized with the callback, all incoming connections will be accepted by default
app.ws?.defineUpgrade(async (req) => {
// you can do some async or sync operation here
// the returning of this function will be
// defined as the `UserClient` and attached to the `ws.Client` instance
return { access: true, client: {...properties of the client} };
});
An instance of the request passed through to middlewares and handlers
Access to the CookiesManager instance attached to the request.
Internal methods to work with cookies
Get an exact header stored to return with this request to the client
Get an exact client cookie of this request
Get all cookies of this request
Imperatively disables cache, does the same as disableCache: true
in RouteOptions
Imperatively sets the cache, does the same as cache: CacheOptions
in RouteOptions
Adds a header outgoing header string as a (key, value) addHeader(header, value)
. Can not overwrite.
Adds outgoing headers in a "batch", merges provided headers object { [header: string]: value }
to already existing headers. Can not overwrite.
Does the same as addHeader
but overrides the value.
Does the same as addHeaders
but overrides the value.
Set a status for the response
Set a status for the response and send it (end the request)
Stream (pipe) a response to the client
Send a JSON response to the client (will attempt to stringify safely a provided object to JSON)
Send a text/buffer response to the client
Some of the features are in progress.
- Request wide context storage and incorporated tracing (ready for cloud)
- UID (adopted or generated)
- WebSocket server exposure
- handy WebSocket interaction tools, for authorization, etc.
- Request-Processing-Time header and value
- Promised or conventional middlewares
- Logging and serialized with
pino
- Routes usage report and endpoint
- Cache headers handy handling, per route
- Cookies creation/parsing
- CORS middleware options
- Request execution cancellation by timeout
- Bulk/chaining routes declaration
- Runtime routes hot swapping
- runtime validation schema generation per route response types (on project compile/on launch)
- middlewares per route
- swagger OpenAPI 3.0 on
/docs
endpoint - swagger OpenAPI 3.0 scheme on
/docs_raw
endpoint - optional export of generated schema to a location (yaml, json)
- streaming/receiving of chunked multipart
- runtime route params or query validation upon declared types (on project compile/on launch)
This feature enables a runtime check for the returned value for a route,
for now it only works for return
statements of the routes declared in handlers.
Please write your return
statements with plain response objects within the handler
or controller
function.
To enable this feature you need to set up the following:
- On
BareHttp
settings set:enableSchemaValidation: true
- On
BareHttp
settings set:declaredRoutesPaths: [...array of paths to routes]
, - On
route.<method>
declaration set:options: { builtInRuntime: { output: true } }
Done on MacBook Pro with M1 Pro processor. No logs enabled. NODE_ENV=production
is set. All settings set to default.
Please open an issue if you have any questions or need support
Licensed under MIT.
Konstantin Knyazev