Using Node.js Module

Using Node.js Core Modules

webOS TV supports for all the Node.js core modules provided by Node.js v0.10.25. For core modules list and API details, see Node.js v0.10.25 Manual & Documentation.

 

To use Node.js core module, add the module with top level identifier as following code in your service

var Service = require(‘webos-service’);
var fs = require(‘fs’);

var service = new Service("com.palm.service.helloworld");

service.register(“writeFile", function(message) {  
  fs.writeFileSync('/media/internal/foobar.txt', 'sample text', "utf8");
  message.respond({
    reply: “write complete"
  });
});

service.register(“readFile", function(message) {  
  fs.readFileSync('/media/internal/foobar.txt', "utf8");
  message.respond({
    reply: “read complete"
  });
});

 

See File I/O Service to refer to sample code using Node.js core module. 

Using Third-party Modules 

There are many third-party modules for Node. To use the third-party modules, follow the next steps.

 

  1. Use npm (Node Package Manager) tool in your computer to install the third-party module. The module will be installed in node_modules directory under node.js installation folder. 
    See Node Packaged Modules (NPM) for search and installation of these modules.
  2. Create a node_modules directory in your service's root directory. 
  3. Copy modules from node_modules directory under node.js folder to node_modules under your service's root directory.

Now, add third-party module as following code in your service. Node will find module from node_modules folder first.  

var md = require('md5');
You cannot use modules including C/C++ addons. You must use modules implemented with JavaScript only.

 

Navigation