Getting started
On our platform a Node.js module can be run as-a-service. It is similar to a microservice. All exported asynchronous functions of a module are transformed by our platform as API calls of a microservice.
Before deployment we build the ready-to-deploy module and we generate a client module
that can be imported in any JavaScript client to call remotely the hosted module.
In this quick tutorial, you'll learn how to create and deploy on a cloud your own Node.js module you can call from a Node.Js script. You'll do all the steps from scratch.
Note that if you're interested in creating an HTTP Node.js server (using Express for example), look at deploy your first Node.js HTTP server in less than 2 minutes.
1. Prepare the project
Let's start from our ready-to-use project, and go directly to the step 4
git clone https://github.com/ScaleDynamics/module my-module
cd my-module/
npm install
or,
Let's create manually your own my-module
working directory, and follow the next steps
mkdir my-module
mkdir my-module/client
cd my-module/
2. Create an empty module
Create a package.json
file
npm init -y
Create index.js
module script
In this example, we are simply writing a hello
function that returns some text, with the Node.js version.
Create a new index.js
file in the module directory, and copy-paste the following code into it:
// my-module/index.js
exports.hello = function (name) {
return `Hello ${name} from Node.js ${process.version}`;
};
3. Install the ScaleDynamics SDK
To install the SDK you need Node.js installed on your computer.
Look at https://nodejs.org/en/download/ to install it.
Install SDK to access all CLI commands:
npm install warp --save-dev
To access our CLI, use npx warp
. You can have the list of available commands and help with
npx warp help
4. Build and generate the client module
Now that the module is ready, we can test it locally. No account is required, you can use the anonymous mode we provide. Just select anonymous when the CLI ask you to choose a project.
npx warp build --output umd:./client/client.js
5. Create a simple Node.js script to call the module
Now the module is build and ready to be used, let's call it from a Node.js script.
Install engine
Install the @warpjs/engine
npm module that is required to manage your client module calls.
npm install @warpjs/engine
create Node.js script
Create a script.js
file containing the following codes:
require('@warpjs/engine');
const MicroService = require('./client/client.js');
const { hello } = new MicroService({ env: 'demo' });
hello('World').then((message) => {
console.log(message);
});
Call module
node script.js
6. Create the project and the environment
Login to your account
To access projects and deployment resources you need a ScaleDynamics account. You can sign up here to create your account. Subscription is FREE, no credit card required.
Once your account is created, you can login to your account with your email and password:
npx warp login
Create a project
A project is a name that identifies a website, a web app, a microservice or an API.
Let's create a module
project, and let's indicate the SDK we are working in it.
npx warp project create module
npx warp project select module
Create an environment
An environment is a name that identifies a cloud execution configuration to run modules. For example you can have 'pre-prod', 'demo', 'staging' or 'production' environments. Each one will have it's own cloud resource configuration.
Let's create a demo
environment, and let's indicate the SDK we are working in it.
npx warp env create demo
npx warp env select demo
7. Setup services for the environment
Before deploying you need to enable the Managed Node.js module service and assign cloud resources to it. To do that, open the console, select the organization, the module project and the demo environment. Then enable the Managed Node.js module service and assign a Shared Free cloud resource on the provider and the region you choose.
After subscribing resources, you will see in the console your services configurations.
8. Deploy the module
Since the build was done before, you can deploy it:
npx warp deploy
During the deployment, you'll see the deployment steps running.
Chapeau!
Congrats', you deployed your first Node.js module-as-a-service. Want to continue?