# cocaine

> Node.js framework for Cocaine platform

Latest version **0.12.1-r16** (published 2015-10-12) · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.12.1-r16 |
| Published | 2015-10-12 |
| First published | 2013-08-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | 0.8.0 - 4.* |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Author | Cocaine Project |
| Maintainers | diunko |

## Links

- npm: https://www.npmjs.com/package/cocaine
- Repository: https://github.com/cocaine/cocaine-framework-nodejs
- Issues: https://github.com/cocaine/cocaine-framework-nodejs/issues
- npm.io page: https://npm.io/package/cocaine

## Dependencies (5)

- [q](https://npm.io/package/q.md) 1.*
- [hexy](https://npm.io/package/hexy.md) ^0.2.6
- [debug](https://npm.io/package/debug.md) *
- [optimist](https://npm.io/package/optimist.md) ~0.4
- [msgpack-bin](https://npm.io/package/msgpack-bin.md) 0.3.0-1

## Recent versions

- 0.12.1-r16 (latest) — 2015-10-12
- 0.12.1-r18 (unstable) — 2016-03-17
- 0.12.1-r09-stripped (stripped) — 2015-08-07
- 0.4.1-6dbg (0.4.1-6dbg) — 2014-09-02
- 0.4.1-5dbg (0.4.1-5dbg) — 2014-09-02
- 0.4.1-4dbg (0.4.1-4dbg) — 2014-09-01
- 0.4.1-3dbg (0.4.1-3dbg) — 2014-09-01
- 0.12.1-r17 — 2015-10-15
- 0.12.1-r15 — 2015-10-12
- 0.12.1-r14 — 2015-10-08
- 0.12.1-r13 — 2015-08-12
- 0.12.1-r12 — 2015-08-10
- 0.12.1-r11 — 2015-08-07
- 0.12.1-r10 — 2015-08-07
- 0.12.1-r09 — 2015-08-05
- … 58 more at https://npm.io/package/cocaine/versions

## README

# Cocaine NodeJS Framework

## Examples of usage

### Create NodeJS app for Cocaine cloud

Let's start with simple NodeJS http application.

```js
var http = require('http')

var server = new http.Server(function(req, res){
    var body = []
    req.on('data', function(data){
        body.push(data)
    })
    req.on('end', function(){
        res.writeHead(200, {
          'x-any-header': 'x-any-value',
          'content-type': 'text/plain'
        })
        res.end('hello, Cocaine!')
    })
})

server.listen(8080)
```

To get our app working in Cocaine cloud, let's add just a couple of things.

```js
#!/path/to/node
var cocaine = require('cocaine')
var http = cocaine.http // monkey-patches node's original http server

var argv = require('optimist').argv //which is actually a hash
// looking like { opt: 'value'}

var worker = new cocaine.Worker(argv)

var handle = worker.getListenHandle("http") // the handle implements a
// low-level nodejs' listening tcp socket, and it makes nodejs
// understand cocaine streams.

var server = new http.Server(...) // the same thing as above

server.listen(handle) // as per [1], start listening on cocaine handle
```

To let the cocaine-runtime know what to run in our app, we put
manifest.json:

```js
{ "slave":"app.js" }
```

Since the app.js has to be an executable, we put shebang on first line
and don't forget about setting an executable bit.

See the complete app here [2].

### Deploy app to the cloud

```bash
git clone url/the_app
cd the_app
npm install
tar -czf ../the_app.tgz
cocaine-tool app upload -n the_app --package ../the_app.tgz --manifest manifest.json
>app the_app has been successfully uploaded
```

then,

```bash
cocaine-tool app start -n the_app -r default
>app the_app started
curl -v http://<cloud.front>/the_app/http/
>...
```

### Make use of Cocaine services

```js

var cocaine = require("cocaine")

var cli = new cocaine.Client(["localhost", 10053])

var log = new cli.Logger("myprefix") // logs lines like "myprefix/..."

cli.on('error', function(err){
    console.log('client error', err)
})

log.on('error', function(err){
    console.log('logger error', err)
})


log.connect()

log.on("connect", function() {

    cli.getServices(['geobase'], function(err, geo, ua){
        var names
        
        log.info("looking up regionId for ip 1.2.3.4")
        
        geo.region_id("1.2.3.4", function(err, regionId) {
            if(err) return _handleError(err)

            log.debug("found region %d for %s", regionId, "1.2.3.4")

            geo.names(regionId, function(err, names){
                if(err) return _handleError(err)

                log.debug("names for region %d are %s", regionId, names.join())

                geo.coordinates(regionId, function(coords){
                    if(err) return _handleError(err)

                    log.debug('coordinates for region %d are %s', regionId, coords.join())

                })
            })
        })
    })
})

function _handleError(err){
    console.log('service error', err)
}
```

See
 [client-simple](http://github.com/cocaine/cocaine-framework-nodejs/blob/master/sample/client.0.js)
 for complete source of the simplest cocaine client app.

### Use Cocaine services from the outside of the cloud

To fully control a client to services, you can use
Client. It resolves services for you, keeps services cache, and resets
resolved services cache on locator disconnect.


```js
var cli = new require('cocaine').Client()

var storage = cli.Service('storage')

storage.on('error', function(err){
    // reconnect on network error
})

storage.connect()

storage.on('connect', function(){
    storage0.write('collection','key','value', function(err){
        if(err){
            console.log('error writing to storage', err)
            return
        }
        
        console.log(done 'writing to storage')
    })
})
```

See [client-reconnect](http://github.com/cocaine/cocaine-framework-nodejs/sample/client.1.js)
for example of handling various socket-level failures when connecting
and communicating to locator and target services.

### Access your application as a Cocaine service

```js
var cli = new require('cocaine').Client()
var app = cli.Service('the_app')

app.connect()

app.on('connect', function(){
    app.enqueue('handle','anydata', function(err, result){
        if(err) {
           console.log('app error', err)
        } else {
          console.log('app response is', result)
        }
    })
})

```

## References

[1] http://nodejs.org/api/net.html#net_server_listen_handle_callback

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