Skip to main content

Node.js

Getting an application running on Boltic is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Boltic global application platform.

In this guide we’ll learn how to deploy a Node.js application on Boltic.

Note

Before starting this guide, ensure you have a Boltic account and have created a serverless app in your account.

Initial Local Setup

Make sure that Node.js is already installed on your computer along with a way to manage dependencies using npm or yarn.

This allows you to run your project locally, and test that it works, before deploying it to Boltic.

tip

We recommend using the latest LTS version of Node.js.

Initialize the Project

Create a new directory for your application and navigate into it:

# Create a new project directory
mkdir my-new-app

# Navigate to the project directory
cd my-new-app

Initialize your Node.js project by running:

npm init -y

This will create a package.json file in your project directory with default values.

info

Since we are using ESM modules in this guide, you need to add "type": "module" to your package.json file. This can be done manually by editing the package.json file and adding:

"type": "module"

or by running:

npm pkg set type="module"
Note

By default all NodeJS app as expected to use ESM syntax. If you would like to use CommonJS syntax, refer the setting in the boltic.yaml file.

Run an Express App

In this guide we recreate and deploy this minimal Express.js application to demonstrate how quickly Express.js apps can be deployed to Boltic!

tip

You can follow the guide to recreate the app or just git clone https://github.com/bolticio/serverless-samples.git to get a local copy.

Make sure to navigate to appropriate directory using: cd serverless-samples/nodejs/applications/docker/hello-world

We assume you already have Node.js installed.

Install Dependencies

Express.js

If you are building an Express.js application, you can install Express.js as a dependency:

npm install express --save

Other Frameworks

For other Node.js frameworks like Nest.js, Koa.js, or Sails.js, install their respective dependencies similarly.

Create the Express App

Create a file named index.js in your project directory and add the following code:

// index.js
import express from 'express';

const app = express();

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}

app.get('*', (req, res) => {
// Extract the path from the request URL
const path = capitalizeFirstLetter(req.path.substring(1)) || 'World';

// Send a JSON response with the path included in the message
res.json({ message: \`Hello, \${path}!\` });
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(\`Server is running on http://localhost:\${PORT}\`);
});

Start the Development Server

Run the following command to start the Express.js server:

node index.js
Server is running on http://localhost:8080

If you open http://127.0.0.1:8080/ in your web browser, it displays Hello, World!.

If you open, for example, http://127.0.0.1:8080/fynd , it displays Hello, Fynd!.

Before Deployment

In order to deploy the app to Boltic, you need to create a boltic.yaml/boltic.toml file in the root of your project. This file contains the configuration for the deployment. Read more about the Application Configuration for more information.

app: nodejs-docker-app-sample
region: asia-south1
entrypoint: "index.js"

build:
builtin: dockerfile
ignorefile: .gitignore

env:
PORT: '8080'

The system will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Boltic service. The rest of the file contains settings to be applied to the application when it deploys:

  • primary_region: it configures where the primary region is, used to create the application;
  • builtin: it configures the builder to use the built-in Dockerfile strategy, which uses a system generated generic Dockerfile to build the application;
  • env: it configures the environment variables to set in the application.
info

This application is being built using the builtin strategy. This strategy uses a system-generated generic Dockerfile to build the application. The ignorefile key is used to specify the name of the file that contains the list of files and directories to be ignored during the build process. The ignorefile key is optional and if not provided, the default value is .dockerignore. For more information on the available builders in Boltic, check out the Builders page.

If you would rather like to use buildpacks to build your application, you can use the following configuration:

app: nodejs-docker-app-sample
region: asia-south1
entrypoint: index.js

build:
builtin: dockerfile
ignorefile: .gitignore

env:
PORT: '8080'
info

This application will now be built using the paketo-buildpacks/nodejs buildpack. Learn more about Paketo Buildpacks.

Now, let’s move on to deploying this app to Boltic.

Deploy to Boltic

We are now ready to deploy our app to Boltic. Ensure you have a Boltic account and have created a serverless app in your account.

tip

In order to clone the repository created for your app after registering it on the console, make sure you have added an SSH key to your Boltic account. You can find the instructions on how to add an SSH key in the SSH Keys section.

Initialize a new git repository in your project:

git init --initial-branch=main

Add a new origin to your local git repository:

git remote add boltic GIT_URL_DISPLAYED_IN_THE_CONSOLE

Commit your changes and push them to the bolt remote:

git add .
git commit -m "serverless initialized"
git push bolt main

The deployment process will start automatically. You can check the status of the deployment in the Boltic console. If everything goes well, the deployment will finish successfully in a few minutes. You can also check the logs to see the if there are any errors. Once the deployment is finished, you can access your app through the URL provided in the Boltic console.

info

The URL will be in the format https://<region>.api.boltic.io/apps/<system-version>/<org-id>/<app-id>.

Run a Function

In this guide we'll create a simple Node.js function to demonstrate how easy it is to set up serverless JavaScript functions on Boltic!

tip

You can follow the guide to recreate the app or just git clone https://github.com/bolticio/serverless-samples.git to get a local copy.

Make sure to navigate to appropriate directory using: cd serverless-samples/nodejs/functions/docker/hello-world

note

Currently there is not native way to test serverless functions locally. You can test the function by directly deploying it to Boltic.

We assume you already have Node.js installed.

Create the Function

The hello-world application is, as you’d expect for an example, very minimal.

Create a new file handler.js and add the following code:

// handler.js
// Define the handler function
export const handler = async (event, context) => {
try {
// Prepare the response JSON
const responseJson = {
message: "Hello World"
};

// Set the response headers
context.setHeader('Content-Type', 'application/json');

// Send the response JSON
context.end(JSON.stringify(responseJson));
} catch (error) {
// Handle errors
console.error(error);
// Send an error response if needed
context.statusCode = 500;
context.setHeader('Content-Type', 'text/plain');
context.end('Internal Server Error');
}
};

You can add custom packages and dependencies to your function as needed. Refer Initialize the Project to init npm in the function directory and install the required packages.

Before Deployment

A few more steps are necessary before deploying the function.

We will define the entry point of the function using handler, which is the function that will be called when the application is invoked. The handler should be in the format module_name.function_name. The module_name is the name of the file where the function is defined and the function_name is the name of the function that will be called. Checkout handler for now information

app: nodejs-buildpack-func-sample
region: asia-south1
handler: "handler.handler"

build:
builtin: dockerfile
ignorefile: .gitignore

Testing the function locally

To test any boltic serverless function locally please refer boltctl serverless test command.

Deploy to Boltic

Deployment of the function is similar to deploying an application. You can deploy the function by following the same steps as mentioned in the Deploy to Boltic section.

Congrats! You have successfully built, deployed, and connected to your first Node.js application/function on Boltic. 🚀