Skip to content

pnwlady/401-http-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

waka-flocka-frame

Waka Flocka Frame HTTP Framework

A simple http framework to take mundane out of the mundane. Quickly create a basic http server with essential router functions by typing just a few lines of code.

Features

  • HTTP Server
  • REST request, response
  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • Automatic Content-Type Detection for Headers

Installation

NOTE: This app requires your computer to have Node js v4.x.x installed. ---------------------

Initialize your local repository

``` npm init ```

Install WFF

``` npm install --save waka-flocka-frame ```

Then require ('waka-flocka-frame') in your project.

var wff = require('waka-flocka-frame');


Code to quickly set up a http server

``` var wff = require('waka-flocka-frame');

wff.server.listen([optional port number]);

Leaving out the port number will invoke the default port 3000.

<h2>Code to quickly set up a simple route.</h2>

wff.router.get('/home', 'a text string');

This will return 'a text string' at the '/home' route.<br>
Syntax:

wff.router.get( '[your route]', '[a text string]');

If you want a response other than plain text you MUST use a callback
and response.writeHead.

wff.router.get('/home', function(req, res){ res.writeHead(200, {'Content-Type': 'application/json'}); res.write(JSON.stringify({"msg": "my message"})); res.end(); });

<h2>Code to quickly set up a static route.</h2>

wff.router.getStatic('someFolder/someText.txt', '/test');

This will return the contents of the 'someText.txt' file at the '/test' route.<br>
Syntax:

wff.router.getStatic('[your file path]', '[your route]');

Parameters are a path to a resource and a route.

NOTE:<i>You may get an error if you add a leading '/' to your file path statement.</i>

<h2>Code to quickly set up a POST route without a callback.</h2>

wff.router.post('/myBlog');

This will allow you to use a REST client (i.e. 'cURL', 'Postman') to post JSON content to the '/myBlog' route.<br>
Syntax:

wff.router.post('[url you are posting to]');

Parameter is the route you are posting to.

<h2>Example of a POST route with an optional callback.</h2>

wff.router.post('/postUrl', function(req, res) { res.writeHead(200, { 'Content-Type': 'application/json' }); req.on('data', (data) => { var path = 'data/' + Date() + '.json'; res.write(data); fs.writeFile(path, data, (err) => { if (err) return 'Error'; console.log(Date() + ' file saved.'); return res.end(); }); }); });

About

Basic http server framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published