0.1.0 • Published 9 years ago

sea-d45-framework v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

Framework is a package for making http routing easier. It currently has full functionality for http server creation, get requests, and handling http, css, and plain text responses.

lib/router.js is responsible for keeping track of the routes once they are created. The module exports a constructor function, which will create a new router object. The router object has a property .routes, which contains the following:

'GET': {}, 'POST': {}, 'PUT': {}, 'PATCH': {}, 'DELETE': {}

Each of these empty objects will store the routing information for that call.

The router has a method for each type of call that will add a route to the object. For example:

Router.prototype.get = function(route, cb) { this.routes'GET' = cb; };

The router also has a 404 method, which will return the status 404 and write 'not found' to the client.

Router.prototype.route is a function which is called when the http server runs its listener function. It takes two parameters, req and res, which are fed to it by the server. The function:

Router.prototype.route = function(req, res) { (this.routesreq.method || this.fourOhFour)(req, res); };

This looks up the callback function stored in the routes object for whatever request is being made. If the route does not exist, the function runs the 404 method.

In lib/app.js, an http server is created with the App.prototype.listen method.

App.prototype.listen = function(port) {

var server = http.createServer(function(req, res) { router.route(req, res); });

server.listen(port, function() { console.log('server running on port ' + port); }); };

Once the server is created on the user-specified port, it will print to the console that the server is up and running. When a request is made, the request listener function calls router.route. router.route checks if there is a route with that url for that request method, and if it exists it returns the callback function defined by the call to router.get (or the equivalent method for the other types of requests).

In app.js, there is a function App.prototype.makeRoute. makeRoute takes three parameters, a route, a request method, and the desired response data. It uses the request method (reqMethod) to call the corresponding function on the router object and define a callback function for when the request is made.

The callback function currently works with GET requests but could be expanded to handle each type of request. Currently this module automatically checks if the response is going to be of content-type 'text/html' or 'text/css', and if not it will default to 'text/plain'. It then writes a status of 200 and the content type to the header, and writes the response data to the client. If the response data is a path to an HTML or CSS file, makeRoute will read the file and pass it into res.write.

This module only has full support for HTML, CSS, and plain text but could easily be expanded to support many types of content and different requests.

When when creating a new instance of the framework, you may specify a public folder for serving static files:

var app = new Framework(__dirname + '/public');

To send responses, you can use the app.sendRes functionality:

app.sendRes(req, res, message);

If a message is entered, the message is sent as the response.

If no message is entered, the file corresponding to the url is found in the public folder and sent as a response. Example:

var app = new Framework(__dirname + '/public');

app.get('/index.html', function(req, res) { app.sendRes(req, res); // sends file 'index.html' from public folder });

An example usage of the framework:

var Framework = require('sea-d45-framework'); var app = new Framework(__dirname + '/public');

// set get route for '/' app.get('/', function(req, res) { // your code goes here });

// set post route for '/greet' app.post('/greet', function(req, res) { // your code goes here });

// set route for get request to '/index' // will send file from path specified in third argument app.makeRoute('/index', 'get', 'public/index.html');

// start server listening on port 3000 app.listen(3000);

This project makes use of sea-d45-router: https://github.com/codefellows/sea-d45-router.