1.1.42 • Published 4 years ago

z-http v1.1.42

Weekly downloads
40
License
ISC
Repository
-
Last release
4 years ago

this is a part of ZadaheaD's owner core tools for easier much more efficiant way to work with HTML, CSS, and Javascripts

look at z-views, if you want a lightweight "ejs like" server-side rendering engine

I never liked working with ejs, or these kind of tools cos I find them very messy, and I always liked working as clean and as seperated as possible. so in this project, I will use the same logic as ejs but with much cleaner way to handle HTML files and manipulate JS objects.

About - Installation

Install z-UIKit, A server side rendering Framework aimed to make front-end development much smoother and flexible with one code base to handle all.

This documentation is designed to start a project from scratch with nothing but npm init

the first thing to install is the http-server for the project.

z-http

z-http is designed for building web applications and APIs.

To install, run npm install for your package.json dependencies:

npm install z-http

to run the server

var http = require('z-http');
http.start(3000, function (route, method) {
    return (data, callback) => {
        callback(`Hello World`);
    }
}, {
//params will go here
});

response for: http://localhost:3000/

Hello World

to get information about method, route, payload or querystring use:

var http = require('z-http');

http.start(3000, function (route, method) {
    return (data, callback) => {
        callback(`
            A ${method.toUpperCase()} request
            for <b>/${route}</b> route
            has been called. <br />

            payload: ${JSON.stringify(data.payload)} <br />
            querystring: ${JSON.stringify(data.querystring)}
        `);
    }
}, {});

response for: http://localhost:3000/home?hello=world

A GET request for /home route has been called.
payload: {}
querystring: {"hello":"world"}

In order to validate request, you can add validation parameter

var http = require('z-http');

http.start(3000, function (route, method) {
    return (data, callback) => {
        callback(`
            A ${method.toUpperCase()} request
            for <b>/${route}</b> route
            has been called. <br />

            payload: ${JSON.stringify(data.payload)} <br />
            querystring: ${JSON.stringify(data.querystring)} 
        `);
    }
}, {
    validation: function (data, callback) {

        this.data = data;
        this.callback = callback;

        this.trig = () => {
            //=>> validate request here
            console.log("headers", data.headers);
            console.log("cookies", data.cookies);

            console.log("querystring", data.querystring);
            console.log("payload", data.payload);
 
            //if valid
            //callback(data);

            //if not valid
            callback(data, {}, errorRoute);
        }
    }
});

const errorRoute = (data, callback) => {
 //fallback function for error requests
 callback("this is an unauthorized request", http.STATUS.UNAUTHORIZED)
}

In order to pass decoded data (like user info) to the request, you can pass it in the validation section

var http = require('z-http');

http.start(3000, function (route, method) {
    return (data, callback) => {
        callback(`
            A ${method.toUpperCase()} request
            for <b>/${route}</b> route
            has been called. <br />

            decoded data: ${JSON.stringify(data.decode)}
        `);
    }
}, {
    validation: function (data, callback) {

        this.data = data;
        this.callback = callback;

        this.trig = () => {

            //pass info from second param
            callback(data, {
                id: 1,
                name: "Mosh"
            });
        }
    }
});

Additional params you can use to define server

http.start(3000, function (route, method) {
    return (data, callback) => {
        callback(`Hello World`);
    }
}, {
   public: 'assets', //public folder for js, css etc - this will load files automatically without routes
   maxFileSize: 500 * 1024 * 1024, //allowed file size is set to 500 MB
   onresponse: (resp, status) => {
      //event triggered for each response sent
      console.log("resp", resp); //the response value
      console.log("status", status); //the response status code
   },
   userBuffer: true, //use buffer (default: false)
   contentTpe: http.CONTENT_TYPE.JSON //set content type (default: text/html)
});

CONST Types

http.STATUS = {
  OK: 200,
  NO_CONTENT: 204,
  REDIRECT_URI: 308,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  TOO_MANY: 429
};
http.CONTENT_TYPE = {
  HTML: 'html',
  CSS: 'css',
  JS: 'js',
  ICO: 'ico',
  JPEG: 'jpg',
  PNG: 'png',
  WOFF2: 'woff2',
  WOFF: 'woff',
  TTF: 'ttf',
  EOT: 'eot',
  SVG: 'svg',
  JSON: 'json',
  XLSX: 'xlsx'
}
1.1.42

4 years ago

1.1.41

5 years ago

1.1.40

5 years ago

1.1.39

5 years ago

1.1.38

5 years ago

1.1.37

5 years ago

1.1.36

5 years ago

1.1.35

5 years ago

1.1.34

5 years ago

1.1.33

5 years ago

1.1.32

5 years ago

1.1.31

5 years ago

1.1.30

6 years ago

1.1.29

6 years ago

1.1.28

6 years ago

1.1.27

6 years ago

1.1.26

6 years ago

1.1.25

6 years ago

1.1.24

6 years ago

1.1.23

6 years ago

1.1.22

6 years ago

1.1.21

6 years ago

1.1.20

6 years ago

1.1.19

6 years ago

1.1.18

6 years ago

1.1.17

6 years ago

1.1.16

6 years ago

1.1.15

6 years ago

1.1.14

6 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago