I have some common code and I put that code in my custom node_module now I want to use that module in my rules and custom database connection but before using it, I have to install my module. So how can I do that? Or is there any other way to achieve my goal?
Modules available from rules are restricted to those that can be found here: Can I require? - Search which node modules you can use in webtask.io
We can add popular modules after a thorough security review. However, due to security reasons, we’re unable to add custom modules that are not widely required.
Rules run in a very controlled sandbox powered by Webtask. Webtask does allow you to add custom modules. An option to make things easier for you would be to add your npm modules there, and implement a simple webtask-powered API that your rules and custom db scripts can consume.
Instructions to add your own npm modules can be found here.
Regarding the API implementation, Webtask is a node sandbox and you can create a webserver using packages like express
.
When you go through the creation of a new webtask, you can click “Select a template”:
Then you select the Express template:
With your newly created webtask using Express, you can add any endpoints you want. Here’s an example of a modified webtask based on the Express template:
var express = require('express');
var Webtask = require('webtask-tools');
var bodyParser = require('body-parser');
// if you followed the instructions to add your package
// you can now include it in your webtask code:
//var myPackage = require('my-npm-package');
var app = express();
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.sendStatus(200);
});
app.get('/customEndpoint', function (req, res) {
let paramToProcess = req.query.paramToProcess;
let result = "Hello ";
if (paramToProcess) {
// use your package as needed:
//result = myPackage.doYourThing(paramToProcess)
result += paramToProcess;
return res.send(200, result)
}
res.status(500).send("Error: Add a param to process as ?paramToProcess={string}");
});
module.exports = Webtask.fromExpress(app);
Here you can see it in action: https://wt-mauricio-auth0-com-0.sandbox.auth0-extend.com/API/customEndpoint?paramToProcess=world
Thanks for so much clarification. I only have one two more issues.
- When I add create my webtask using wt cli as mentioned in the provided link, it gives me the url so how can I use that url in var myPackage = require(‘my-npm-package’);
- What is /customEndpoint? How can I get this?
Thanks
I’m afraid I might have confused you with my previous responses. Let’s recap:
At the moment, it’s not possible to add custom npm modules to the sandbox where all rules and custom db scripts run. We can only add very popular modules, like mongoose
.
One alternative for you to re-use common code is to create a simple API in Webtask that your rules and custom DB scripts can call via request
.
Since you already created one Webtask, you can use the wt edit
CLI command to open the web editor where you can customize the Webtask’s code, add Secrets, or NPM modules, among other things. You can use wt edit --help
to find out what parameters to pass.
The code I provided in my previous response is roughly how your Webtask would look like. To use the common npm module that you wrote, you would need to upload it to https://www.npmjs.com/. Then, using the web editor, you need to click the Configuration icon (the cog), and then click NPM Modules
, as shown at the right side of this screenshot:
In the NPM Modules configuration screen, you add your custom NPM module, searching by its name. Once you have added the NPM module, it should be available for use in your code. For example, if you added the mongoose npm module, you would import it in your webtask by adding var mongoose = require('mongoose');
at the top.
What is /customEndpoint? How can I get this?
That is just an example of how to add custom endpoint to your API using Express’ routing. Once you have written your own endpoints, you can call them from rules, like shown in this example.
Can we call our own api/services using request ?
For example, if we wanted to mint custom tokens from firebase.
Hey there!
Sorry for such huge delay in response! We’re doing our best in providing you with best developer support experience out there, but sometimes our bandwidth is not enough comparing to the number of incoming questions.
Wanted to reach out to know if you still require further assistance?