Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tomahawk

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tomahawk

Minimal HTTP Server

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.7K
decreased by-16.04%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status dependency Status devDependency Status NPM version

Tomahawk

Tomahawk provide you with a minimal http server, that can be started on the command line to serve static content.

To install

npm install -g tomahawk

How to use the Tomahawk web server

  • To serve the current directory using the default port 8080

    tomahawk

  • To serve "web/public" from the current directory using the default port 8080

    tomahawk --www=web/public

  • To use a different port (e.g. 3000)

    tomahawk --port=3000

  • How can I prefix my static content with MyApp (e.g. http://localhost:8080/MyApp insead of the default http://localhost:8080/)

    tomahawk --context=/MyApp

  • How about less logging?

    tomahawk --level=error

How can I add CORS http headers

tomahawk --config=config.development.json

or

tomahawk --headers='["Access-Control-Allow-Origin:*","Access-Control-Allow-Methods:GET,PUT,POST,DELETE","Access-Control-Allow-Headers:Content-Type","X-Frame-Options:ALLOWALL"]'

start a CGI

tomahawk --cgi='[{"route":"/echo","method":"POST","command":"/bin/sh", "args":["-c", "read line ; echo $line"], "encoding":"utf8"}]'
echo allo | curl -X POST -d @- http://localhost:8080/echo
  • Can I save the configration, to avoid typing it every time?

    1. Either create a config.json and save it in the current working directory
    2. Create your configuration file (e.g. tommy.cfg) and : tomahawk --config=tommy.cfg or export CONFIG=tommy.cfg tomahawk

    Your configuration file should look like:

      {
          "level"      : "error",
          "www"        : ".",
          "context"    : "/MyApp",
          "port"       : 9000,
          "headers"    : [
              "Access-Control-Allow-Origin:*",
              "X-Frame-Options:ALLOWALL"
          ],
          "cgi" : [
              {
                  "route"   : "/version",
              	"method"  : "GET",
              	"command" : "/bin/sh",
              	"args"    : ["-c",  "echo '{\"version\":\"1.0.0\"}'"]
              }
          ]
       }
    

How can I include tomahawk into my project as a module to server REST routes?

You can look at the sample code under the sample directory:

samples/sample_server.js:

var app = require('tomahawk').create({port:8080,routes:[__dirname + '/lib/routes']}).start();

samples/lib/routes.js':

module.exports = function () {

    function routes(app, config, io) {
        var database = {
            captains : {
                "jim"    : "James Tiberius Kirk",
                "picard" : "Jean-Luc Picard"
            },
            starShips : {
                "jim": "NCC1701-A",
                "picard": "NCC1701-D"
            }
        };

        // GET
        app.get('/api/v1/captain/:id?', function (req, res) {
            var withStarship = req.query.starship === 'true';
            if (req.params.id) {
                res.json(withStarship ?
                {id:req.params.id,name:database.captains[req.params.id], starship:database.starShips[req.params.id]} :
                {id:req.params.id,name:database.captains[req.params.id]});
            } else {
                res.json(database.captains);
            }
            res.end();
        });

        // PUT
        app.put('/api/v1/captain/:id', function (req, res) {
            database.captains[req.params.id] = req.body;
            io.sockets.emit('new:captain', {id:req.params.id, name:req.body});  // Optional, if you want to use websocket
            res.json({id:req.params.id,operation:"put",status:"OK"});
            res.end();
        });

        // DELETE
        app.delete('/api/v1/captain/:id', function (req, res) {
            delete database.captains[req.params.id];
            io.sockets.emit('del:captain', {id:req.params.id}); // Optional, if you want to use websocket
            res.json({id:req.params.id,operation:"delete", status:"OK"});
            res.end();
        });
    }

    return routes;
}();    

Run:

node sample_server.js

FAQs

Package last updated on 16 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc