Skip to content

Netbeast router applications

pablopi edited this page Nov 3, 2015 · 4 revisions

An Netbeast app is basically an npm node.js package. To run a minimalist app you need to create a package.json and an entry point, lets call it server.js. The entry point (server.js in this case) must be executable.

./myapp
    |-- index.html
    |-- server.js
    |-- package.json
    |-- node_modules/

In package.json you must specify the entry point at mainfield:

{
  "name": "nb-get-started",
  "version": "1.0.0",
  "description": "A repo with code about how to make simple Netbeast apps.",
  "main": "server.js",
  "dependencies": {
    "colors": "^1.0.3",
    "minimist": "^1.1.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "node server.js --port 31416",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/netbeast/get-started"
  },
  "keywords": [
    "iot",
    "netbeast",
    "netbeast.co"
  ],
  "author": "jesus@netbeast.co",
  "license": "GPL 2",
  "bugs": {
    "url": "https://github.com/netbeast/get-started/issues"
  },
  "homepage": "https://github.com/netbeast/get-started"
}

To make server.js executable you must run:

    chmod +x server.js

And append a shebang at the very beginning of the file

#!/usr/bin/env node

Also if you want to run a service you may want to accept a port by parameters. You can find a sample app at https://github.com/netbeast/get-started, the code inside server.js looks like this:

#!/usr/bin/env node

// server.js
//===========

/*
* This is where all the magic happens.
* The Netbeast dashboard calls this script as is
* `node server.js --port <free port number>`
* after that everyline here will be executed.
*
* You can install extra modules thanks to the work
* of npm. Also you can create a shell script to
* install any missing system package.
*/

/* Requires node.js libraries */
var fs = require('fs');
var express = require('express');
var app = express();

function puts(error, stdout, stderr) { sys.puts(stdout) }

var argv = require('minimist')(process.argv.slice(2)); // must-have package

/* Netbeast os apps need to accept the port to be launched by parameters */
port = argv.port;

if(isNaN(port)) {
	console.log("Port \"" + port + "\" is not a number.");
	process.kill(1);
} else {
	console.log("Listening on port " + port);
}

app.use(express.static('public'));


var server = app.listen(port, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

The nb-dashboard will automatically find a free port and assign it to your application.

Clone this wiki locally