Skip to main content

Node.js server

On our platform you can deploy a standard HTTP Node.js server (i.e. using Express, Koa, Fastify, ...) so you can access it with traditional HTTP requests. You can use your favorite Node.js web framework to host all all your website by serving your static assets and backend, your microservice APIs, a backend proxy... without managing the cloud itself (provisioning, deployment, setup, HTTPS certificates, security patches, cloud change...)

In this quick tutorial, you'll learn how to create and deploy a basic Express HTTP Node.js server you can call from HTTP.

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/server my-server
cd my-server/
npm install

or,

Let's create manually your own my-server working directory, and follow the next steps

mkdir my-server
cd my-server/

2. Create a basic HTTP Express.js Node.js server

Create a package.json file

{
"name": "my-server",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node ."
}
}

Install Express

npm install express

Create index.js

Create a new index.js file in the server directory, and copy-paste the following code into it:

// my-server/index.js

const express = require('express');
const app = express();
const port = process.env.WARP_PORT || 3000;

app.get('/', (req, res) => {
const name = req.query.name || 'World';
const data = {
message: `Hello ${name} from Node.js`,
node: process.version,
date: new Date().toISOString(),
};
res.json(data);
});

app.listen(port, () => {
console.log(`This app listens on the port ${port}`);
});

Note that server must use process.env.WARP_PORT to identify which port to listen.

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 the 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. Create the project and the environment

The deployment of a server module requires to indicate in which project and which environment you want to deploy and execute your module.

A project is a name that identifies a website, a web app, a microservice or an API.

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.

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 identifies a website, a web app, a microservice or an API.

Let's create a server project, and let's indicate the SDK we are working in it.

npx warp project create server
npx warp project select server

Create an environment

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

5. Setup services for the environment

Before deploying you need to enable the Managed Node.js server service and assign cloud resources to run it. To do that, open the console, select the organization, the server project and the demo environment. Then enable the Managed Node.js server 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.

6. Deploy the server

You're now ready to deploy the server module.

npx warp deploy

During the deployment, you will have to indicate the url where you want to access your server after deployment.

✔ Enter a hostname for 'server' (fully qualified or not), leave blank for random: … 

You can enter a name or press return to get a random one.

After deployment the url to use to call your module is dumped on the terminal.

if you want to know the deployment url, you can access the console or use the following command to get it:

npx warp env info

7. Call the server

Now the server is deployed, let's call it from using curl for example:

curl https://DEPLOYMENT_URL

Chapeau!

Congrats', you deployed your first managed Node.js server. Want to continue?