router-framework-haanj
v0.1.3
Published
This is a simple routing framework written as a Code Fellows 401 assignment. It functions as a light wrapper around the node HTTP module.
Downloads
3
Readme
router-framework-haanj
This is a framework made for a Code Fellows 401 assignment. The purpose is to help the user create a RESTFUL server by automatically handling aspects of the HTTP node module.
Usage
Usage is pretty straightforward.
Install to your project directory
npm install -S router-framework-haanj
Require in the module
var server = require('router-framework-haanj')
Create an array of routes you want to create. Include method, route, and callback function
var routes = [
{
method: 'get',
route: '/users',
fn: (req, res) => {
console.log('/users route hit with GET request');
res.writeHead(200, {'content-type': 'text/plain'});
res.write('hello users');
res.end();
}
},
{
method: 'post',
route: '/users',
fn: (req, res) => {
console.log('/users route hit with POST request');
var jsonObject = '';
req.on('data', (data) => {
jsonObject += data;
});
return req.on('end', () => {
console.log(jsonObject);
jsonObject = JSON.parse(jsonObject);
res.writeHead(200, {'content-type': 'text/plain'});
res.write(JSON.stringify('hello ' + jsonObject.name));
return res.end();
});
}
}
];
Start the server by passing in your port and route array.
server(3000, routes)
The module will create the routes and start up the server automatically.