How to Install a Node Module and Call it from a Node Service in a Web Application
Description
Node modules can be downloaded and used in Alpha Anywhere web applications.
Discussion
Web Applications can call methods in Node.js modules from server-side Xbasic. The Node_request() function can be used to call a node service from a web application. For example, suppose you have created the following node module saved as tinify.js inside the node_services directory:
var tinify = require("tinify"); tinify.key = "apikey goes here"; exports.handler = function (packet, response, sendResponse) { var source = tinify.fromFile(packet.infile).resize(packe.resize).toFile(packet.outfile,function(err) { if (err) { response.error = err.message; } sendResponse(response,null); }); };
The node service defined above can be called from Xbasic, such as in an .a5w page, as follows:
<%A5 dim req as p dim req.infile as c = "C:\movieImages\snow.jpg" dim req.outfile as c = "C:\movieImages\snowSmall.jpg" dim req.resize.method as c = "fit" dim req.resize.width as n = 100 dim req.resize.height as n = 200 if file.exists(req.outfile) then file.remove(req.outfile) end if ? node_request("tinify",req) if file.exists(req.outfile) then ? "Thumbnail was created" else ? "Thumbnail was *not* created" end if %>
In the videos below, we demonstrate how to download the tinify Node Module using NPM and add it to your web project. We then show how to create a Node Service to call the tinify package from an .a5w page.
See Also