basehttp

A lightweight, robust node.js HTTP server with configurable options.

basehttp

basehttp improves upon node-router, and remains lightweight, enabling you to speed up web development without compromising response time.

  • settings (object):
    • .static_path (string) - specifies the directory from which to serve static content (relative or absolute)
    • .template_path (string) - specifices the template directory (relative or absolute)
    • .cookie_keys (Keygrip object or array) - cookies
    • .ssl_options (object) - https
  • logger (function) - function which acts output middleware; defaults to console.log
  • .addRoute(method, path, callback)
    • method (string) - specify the HTTP method ('GET', 'POST', 'PUT', 'DEL', 'HEAD')
    • path (string or regular expression) - listen on the requested url, or path
    • callback(req, res, matched) (function) - handles requests matching method and path; callback is passed req, res, and the matched string in the requested URL
  • .get(path, callback) - server.addRoute('GET', path, callback)
  • .post(path, callback) - server.addRoute('POST', path, callback)
  • .put(path, callback) - server.addRoute('PUT', path, callback)
  • .del(path, callback) - server.addRoute('DELETE', path, callback)
  • .head(path, callback) - server.addRoute('HEAD', path, callback)
  • .settings (object) - a copy of the server settings object
  • .authenticated (boolean) - defaults to 'false' unless the user has a cookie matching settings.cookie_name
  • .cookies (object) - new Cookies contsructor
  • .fields (object) - contains the fields parsed within a multipart/form-data request [field_name][value]
  • .files (object) - contains the files parsed within a multipart/form-data request [file_name][file]
  • .settings (object) - a copy of the server settings object
  • .cookies (object) - new Cookies contsructor
  • .send(code, body, content_type, headers)
    • code (number) - valid HTTP status code
    • body (string) - data to send to the client
    • content_type (string) - is the type of data to be sent to the client
    • headers (object) - optional, additional HTTP headers to add to the response
  • .notFound(message)
    • return a 404 status code to the client
    • message (string) - optional message to send with the 404 status code; default is 'Not Found'
  • .serverError(message)
    • return a 500 status code to the client
    • message (string) - optional message to send with the 500 status code; default is 'Server Error'
  • .redirect(location)
    • location (string) - path to redirect the client to
  • .innerRedirect(location)
    • location (string) - path to redirect the client to, internally
  • .render(filepath, templateVars, options)
    • filepath (string) - file to render; if settings.template_path is specified, join settings.template_path with filepath, else resolve filepath
    • templateVars (object) - contains template variables
var server = require('basehttp').createServer();
 
server.get('/', function (reqres) {
    res.send(200, 'Hello, World!', 'text/plain');
});
 
server.listen(8080);