basehttp

A basic HTTP server library.

basehttp

A basic node.js HTTP server library.

  • template_path string - sets the path to templates
  • static_path string - sets the path to static files
  • cookie_keys array - sets the secret cookie keys
  • ssl_options object - sets the key and cert files

In addition to the default HTTP server methods:

  • server.addRoute(method, pattern, handler, format) - add a route handler for a requested path matching pattern
  • server.get(pattern, handler) - shorthand for server.addRoute('GET', pattern, handler)
  • server.post(pattern, handler, format) - shorthand for server.addRoute('POST', pattern, handler, format)
  • server.put(pattern, handler, format) - shorthand for server.addRoute('PUT', pattern, handler, format)
  • server.del(pattern, handler) - shorthand for server.addRoute('DELETE', pattern, handler)
  • server.head(pattern, handler) - shorthand for server.addRoute('HEAD', pattern, handler)
  • server.resource(name, controller, format) - creates a REST resource with common mappings
  • server.resourceController(name, data, on_change) - returns a resource controller with the following methods:
    • index(req, res)
    • show(req, res, id)
    • create(req, res, body)
    • update(req, res, id, body)
    • destroy(req, res, id)

In addition to the default HTTP response methods:

  • res.send(code, body, content_type, headers) - send an HTTP response code, a message, body, the content type, and additional headers
  • res.notFound(message) - send a 404 response code with an optional message
  • res.serverError(message) - send a 500 response code with an optional message
  • res.redirect(location) - send a 302 response code with a location
  • res.innerRedirect(location) - perform an internal redirect
  • res.render(filepath, templateVars) - render a template with optional variables; if the template path is set on the config/settings object for this server instance, this directory will be checked first, then the resolved path
  • (req|res).settings - a copy of the config/settings object passed to this server instance
  • (req|res).cookies - a cookies object if the cookie keys are set on the config/settings object for this server instance
var config = {
        template_path: './templates',
        static_path: './static',
        cookie_keys: ['SEKRIT2', 'SEKRIT1']
    },
    server = require('basehttp').createServer(config);
 
server.get('/', function (reqres) {
    res.cookies.set('basehttp-cookie-name', 'basehttp-cookie-value', {signed: true});
    res.render('index.html');
});
server.listen(8080);