Entrance
You need to pass 4 parameters to the init()
function: request, response, environment object and router map.
- request is the same object from http.createServer callback.
- response is the same object from http.createServer callback.
- env is anything you want to pass to your handler. For example database object.
- map is a string/function mapping const. It indicates which handler function should a request with specific path be mapped to.
const router = ;const file = ; // Your handler moduleconst test = ; // Your handler moduleconst map = '/test/server': testserver '/file/upload': fileupload;const env = database: null // Let's say you want to pass a database to the handler tooconst server = http;server;
Processing
- If the request contentType is application/json, the module will process and transform the body into a json object.
- If the request contentType is text/plain, the module will pass the text directly.
- If the request contentType is almost everything else, the module will assume it's a file and form up an object like this:
Handler
A handler function should always have a signature like this:
{ // Deal with the data}
Depends on the Content-Type in request header, data will be:
- JSON object if Content-Type is "application/json".
- String if Content-Type is "text/plain".
- JSON object listing in the Processing section if Content-Type is anything else.