Tomahawk HTTP Server

0.2.1 · abandoned · verified Sun Apr 19

Tomahawk is a minimalist HTTP server designed for Node.js. It provides functionality for serving static content, executing CGI scripts, and defining REST API routes programmatically. Currently at version 0.2.1, its development appears to be abandoned, with its `engines` field explicitly requiring Node.js versions `>= 0.8.0 < 0.11.0`. Node.js 0.8.0 was released in June 2012, and 0.11.0 in March 2013, making this package incompatible with all modern Node.js runtimes. Its key differentiator was its simplicity and dual nature as both a command-line utility for static/CGI serving and a module for basic REST API creation, offering a lightweight alternative during its active period. Compared to contemporary HTTP server solutions, Tomahawk lacks modern features, performance optimizations, and security updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Tomahawk programmatically to set up a basic HTTP server, serve static files, and define REST API routes (GET, PUT) for a simple in-memory database. It includes an example of routing with path parameters and query parameters.

const tomahawk = require('tomahawk');
const path = require('path');

// A simple database for demonstration
const database = {
  captains : {
    "jim"    : "James Tiberius Kirk",
    "picard" : "Jean-Luc Picard"
  },
  starShips : {
    "jim": "NCC1701-A",
    "picard": "NCC1701-D"
  }
};

// Define routes dynamically
const routesModule = function () {
  function routes(app, config, io) {
    // GET all captains or a specific one
    app.get('/api/v1/captain/:id?', function (req, res) {
      const withStarship = req.query.starship === 'true';
      if (req.params.id) {
        const captainData = database.captains[req.params.id];
        if (captainData) {
          res.json(withStarship ?
            {id: req.params.id, name: captainData, starship: database.starShips[req.params.id]} :
            {id: req.params.id, name: captainData});
        } else {
          res.writeHead(404, {'Content-Type': 'application/json'});
          res.end(JSON.stringify({ error: 'Captain not found' }));
        }
      } else {
        res.json(database.captains);
      }
      res.end();
    });

    // PUT (create/update) a captain
    app.put('/api/v1/captain/:id', function (req, res) {
      // In a real app, req.body would be parsed by middleware
      // For this example, assuming req.body is already a string/object
      const newCaptainName = req.body || req.params.id; // Simplified for demo
      database.captains[req.params.id] = newCaptainName;
      res.json({ status: 'success', id: req.params.id, name: newCaptainName });
      // io.sockets.emit('new:captain', { id: req.params.id, name: newCaptainName }); // Example from original, 'io' is undefined here
      res.end();
    });

    console.log('REST routes registered.');
  }
  return routes;
};

// Create and start the server
const app = tomahawk.create({
  port: process.env.PORT || 8080,
  www: path.join(__dirname, 'public'), // Serve static files from a 'public' directory
  routes: [routesModule()] // Pass the route definitions
}).start();

console.log(`Tomahawk server running on http://localhost:${app.config.port}`);
console.log('Try visiting /api/v1/captain/jim or /api/v1/captain?starship=true');
console.log('To test PUT: curl -X PUT -H "Content-Type: application/json" -d "Arthur Dent" http://localhost:8080/api/v1/captain/arthur');

view raw JSON →