1.0.0 • Published 5 years ago

@jf/server-base v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

jf-server-base stable

npm install jf-server-base

A very simple wrapper for node webserver.

// Receive log and save to disk.
const fs = require('fs');
require('jf-server-base').create(
    8888,
    (request, response, requestBody) => {
        fs.writeFileSync(
            `/tmp/request-${Date.now()}.log`, 
            JSON.stringify(requestBody)
        );
        response.writeHead(201);
        response.end();
    }
);

Callback will receive as third param the request body as object or string so you don't need add listeners for retrieving body because jfServerBase exports other function, named getBody, for getting data of readable streams such as response and request parameters:

const request  = require('http').request(
    {
        // Put request config here
    },
    response => require('jf-server-base').getBody(
        response,
        body => console.log(body)
    )
);
request.write(
    JSON.stringify(data)
);
request.end();