# tomahawk

> Minimal HTTP Server

Latest version **0.2.1** (published 2014-12-16) · 0 weekly downloads

## Install

```sh
npm install tomahawk
pnpm add tomahawk
yarn add tomahawk
bun add tomahawk
```

Provides the command `tomahawk`.

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2014-12-16 |
| First published | 2014-01-11 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.8.0 < 0.11.0 |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+7 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Henri Bouvier |
| Maintainers | hbouvier |

## Links

- npm: https://www.npmjs.com/package/tomahawk
- Repository: https://github.com/hbouvier/tomahawk
- Issues: https://github.com/hbouvier/tomahawk/issues
- npm.io page: https://npm.io/package/tomahawk

## Dependencies (7)

- [morgan](https://npm.io/package/morgan.md) 1.2.0
- [connect](https://npm.io/package/connect.md) 3.0.2
- [express](https://npm.io/package/express.md) 4.6.1
- [winston](https://npm.io/package/winston.md) 0.7.3
- [body-parser](https://npm.io/package/body-parser.md) 1.5.0
- [errorhandler](https://npm.io/package/errorhandler.md) 1.1.1
- [node-options](https://npm.io/package/node-options.md) 0.0.7

## Recent versions

- 0.2.1 (latest) — 2014-12-16
- 0.2.0 — 2014-09-22
- 0.1.6 — 2014-09-16
- 0.1.5 — 2014-09-01
- 0.1.4 — 2014-08-08
- 0.1.3 — 2014-08-07
- 0.1.2 — 2014-07-23
- 0.1.1 — 2014-07-23
- 0.1.0 — 2014-07-23
- 0.0.18 — 2014-07-23
- 0.0.17 — 2014-07-20
- 0.0.16 — 2014-07-20
- 0.0.15 — 2014-07-20
- 0.0.14 — 2014-07-01
- 0.0.13 — 2014-07-01
- … 11 more at https://npm.io/package/tomahawk/versions

## README

[![Build Status](https://travis-ci.org/hbouvier/tomahawk.png)](https://travis-ci.org/hbouvier/tomahawk)
[![dependency Status](https://david-dm.org/hbouvier/tomahawk/status.png?theme=shields.io)](https://david-dm.org/hbouvier/tomahawk#info=dependencies)
[![devDependency Status](https://david-dm.org/hbouvier/tomahawk/dev-status.png?theme=shields.io)](https://david-dm.org/hbouvier/tomahawk#info=devDependencies)
[![NPM version](https://badge.fury.io/js/tomahawk.png)](http://badge.fury.io/js/tomahawk)

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

---
_Source: https://npm.io/package/tomahawk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
