Cocaine NodeJS Framework
Examples of usage
Create NodeJS app for Cocaine cloud
Let's start with simple NodeJS http application.
var http = var server = { var body = req req} server
To get our app working in Cocaine cloud, let's add just a couple of things.
#!/path/to/nodevar cocaine = var http = cocainehttp // monkey-patches node's original http server var argv = argv //which is actually a hash// looking like { opt: 'value'} var worker = argv var handle = worker // the handle implements a// low-level nodejs' listening tcp socket, and it makes nodejs// understand cocaine streams. var server = ... // the same thing as above server // as per [1], start listening on cocaine handle
To let the cocaine-runtime know what to run in our app, we put manifest.json:
"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
git clone url/the_appcd the_appnpm installtar -czf ../the_app.tgzcocaine-tool app upload -n the_app --package ../the_app.tgz --manifest manifest.json>app the_app has been successfully uploaded
then,
cocaine-tool app start -n the_app -r default>app the_app startedcurl -v http://<cloud.front>/the_app/http/>...
Make use of Cocaine services
var cocaine = var cli = "localhost" 10053 var log = "myprefix" // logs lines like "myprefix/..." cli log log log { console}
See client-simple 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.
var cli = 'cocaine' var storage = cli storage storage storage
See client-reconnect for example of handling various socket-level failures when connecting and communicating to locator and target services.
Access your application as a Cocaine service
var cli = 'cocaine'var app = cli app app
References
[1] http://nodejs.org/api/net.html#net_server_listen_handle_callback