z-http
v1.1.42
Published
handle all routs for the app and deliver all type of files
Downloads
10
Readme
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'
}