siuje
v1.1.0
Published
A tiny web-service for nas
Downloads
295
Readme
Siuje
A light framework for http backend server
What is Siuje
It is a very very tiny web server library, for Node.js.
How to Use
Startup the Server
// index.js
const siuje = require("siuje");
siuje.work({ host: "127.0.0.1", port: 3000 })
Schedule a Job
// index.js
const siuje = require("siuje");
siuje.when( "12:00" ).work(
async () => {
// do some work here
}
);
siuje.work({ host: "127.0.0.1", port: 3000 })
or
// index.js
const siuje = require("siuje");
const {second, minute, hour, day} = require("siuje/utime");
siuje.every( second(10) ).repeat(
async () => {
// do something
}
)//.until( () => true|false );
siuje.work({ host: "127.0.0.1", port: 3000 })
Handle a HTTP Request
// index.js
const siuje = require("siuje");
// sample for middleware
const set_header = headers => {
return (req, ctrl) => next => {
ctrl( headers );
return next(req, ctrl);
}
}
siuje.meet("/test")
.use( set_header({"Server": "siuje"}) )
.serve( async (req, ctrl) => {
return "hello, world";
});
siuje.work({ host: "127.0.0.1", port: 3000 })
Middleware Presets
You can use siuje.seq to embed multiple middlewares into one middleware:
// index.js
const siuje = require("./siuje");
// sample for middleware
const set_header = headers => {
return (req, ctrl) => next => {
ctrl( headers );
return next(req, ctrl);
}
}
const my_headers = siuje.seq(
set_header({"connection": "keep-alive"}),
set_header({"access-control-allow-origin": "*"})
);
siuje.meet("/test")
.use( set_header({"Server": "siuje"}) )
.use( my_headers )
.serve( async (req, ctrl) => {
return "hello, world";
});
siuje.work({ host: "127.0.0.1", port: 22181 })
How about bodyParser?
You should implement your middleware for body parser.
Hot to Deploy in Serverless?
You should use siujeless for serverless platform.
Detail for req/ctrl and next
- req is the instance for http.IncomingMessage
- next is only a function to run the next middleware
- ctrl is a handler for building the response
- when you give a number to ctrl, it means that you want to set the http response status code
- when you give an object to ctrl, it means that you want to set the http response header
- when you give a string(json/query/else) to ctrl, it means that you want to set the http response mine type
- when you give an empty to ctrl, it means that you want to get the current status/header and mine type
- when you give an error object to ctrl, it will give a 500 for response
- when you give a function to ctrl, your function will be call with the http response object and a setter to custom your output