How to deploy Node.js functions on Nimbella
copied
Node.js is highly preferred for developing data intensive real-time web applications. In this blog, let’s learn how to deploy Node.js functions on the Nimbella Serverless platform to build web applications.
Please consider that we are going to build a URL Shortener with Node.js for back-end, React.js for front-end and MongoDB as Database.
We are going to follow the below steps to build our app and make it live.
- Create Node.js function and implement logic for URL Shortener.
- Create React.js front-end which will consume the Shortener service created in the above step.
- Set up Nimbella. Log in with nim CLI and deploy both front-end & back-end with deploy command.
Below diagram illustrates how we process the data for our Shortener Service
Back-end:
Initializing Node.js application
Create a directory for the app and navigate to the directory. And then, create an index.js file.
Run the below command to initialize the project and provide the entry point as index.js.
npm init
We need to install NPM packages Mongoose (to interact with MongoDB), and nanoid (to create unique short id).
npm install mongoose
npm install nanoid
Establish connection with MongoDB using mongoose package
Let’s create a model for URL data and establish the connection with MongoDB as below in index.js file. We can get the username, password, and cluster URL from environment variables.
Implementing App Logic
In general, when a request is received by Nimbella for your deployed serverless function, Nimbella will forward it to the main function by passing the request attributes as arguments and returns the response of the function to the client as response.
Redirecting Users
So, let’s create a main function which will deal with the client request. When our function receives a path param, we can conclude that the user is trying to visit the actual URL with a shortened URL. Let’s implement the redirection as below.
Creating Short URLs
When a client request is received to our serverless function without a path param, we can conclude that the user is requesting a short URL with an actual URL. Let’s implement the same as below.
Now the index.js file looks as below after logic implementation.
Now our back-end code with node.js is completed and the directory looks as below
Front-end:
Let’s create a React app as front-end for our URL Shortener with below command
npx create-react-app {my-app-name}
Let’s modify and design our front-end in the usual React.js way and consume our API developed with the above steps using the NPM package “Axios’.
Structuring the Directory for Deployment
As per Nimbella’s Deployment Standard, the back-end code for API has to be placed under a directory named “packages” and front-end code into a directory named "web".
Please note that the directory name represents end points, once deployed into Nimbella. Hence the directory name needs to be created with the name of the end points we would like to have in our API.
So, let’s create a directory named "s" as a reference for the Shortener app and copy the Node.js project created into the directory. Copy the whole directory into the directory named "packages".
Likewise, let's copy the code for the front-end (React project) into the directory named “web”.
Once the directory structure is modified as specified, it looks as below:
Inside the web folder, let's create two files named ".include" and "build.sh".
.include → React app’s build directory is mentioned inside this file which gets deployed to Nimbella.
build.sh → contains the npm command to build the react app.
Project.yml File
In Nimbella, we will be specifying the artifacts to be deployed in a yml configuration file. Let’s create a file named "project.yml" and specify the configuration for deployment as below.
How to Configure Environment variable in Nimbella for deployment
Create an .env file having the values of the environment variables to be used. Environment values will be passed to the functions through Nimbella by specifying the variables in project.yml under the environment section.
Once the above Steps are done, directory structure looks as below having mentioned files.
Set up Nimbella for Deployment
- Create an account in Nimbella and install nim CLI and then log in with below command in nim CLI.
nim login
- Deploy the application in Nimbella with below command in nim CLI
nim project deploy {application’s directory} --env={path of environment file}
- Get URL of the deployed Serverless action with below command in nim CLI
nim action get {deployed action’s name} --url
Hurray! Our API is deployed to the above URL and the front-end is deployed to the host of our API.
Let’s test our API for the Shortener service with Postman
Let’s try our App through UI by visiting the host URL of our API https://pathitha-4mpb1smu66t-apigcp.nimbella.io/
GitHub Source: https://github.com/boltathi24/nimbella_node_url_shortner.git
Guest Author: Athithan Raj P.
Follow him on Twitter: @raj_athithan
Recent Posts
- How to deploy Node.js functions on Nimbella
- Kick-Start Your Serverless Journey
- AWS re:Invent Serverless Highlights
- Opportunities in the Wake of the AWS Juggernaut
- FaaS Wars: Serverless & Virtual Robot Competition
- #DeveloperIPL Online Hackathon Results & Feedback on Nimbella's Integration for Postman
- How to connect to the 3rd party database such as MySQL at Nimbella (example in Java)
- What can you do with the Nimbella Workbench?
- Deploy your Shopify Storefront to Nimbella
- Not All Serverless Platforms Are Created Equal
- Nimbella + Netlify: Uplevel Your Development Speed
- How we learned to Jamstack, Our Caputron Story.
- Commander for Microsoft Teams - Your Custom Bot that runs on your Command!
- How to Build a Stateful Cloud App on Nimbella vs. AWS
- Starter Kit and Resources to Build a Serverless Cloud Application
- How to Build Serverless Slack Apps and Commands
- How to Set up your Serverless Environment and Get Started in Less than 2 Minutes!
- How to Quickly Deploy Stateful Serverless Apps with Nimbella?
- What is Serverless Computing? 3 reasons to start now
- How to Build a Serverless Slack App in Minutes.
- How to Manage your Netlify Website from Slack?
- How to Build a Serverless Slack Command in minutes
- How to Build a Stateful Serverless Cloud Web Application?
- How to Create an Optical Character Recognition (OCR) Application?
- Development at the Speed of Innovation – Nimbella, the Serverless Cloud
- Software Security Features on Enterprise Serverless Slack Apps Enabled by Nimbella Commander
- Coronathon India’s first demo day has 18 projects to help fight COVID-19
- See the time in different cities on Slack with Nimbella Commander
- Greet your friends in their native language in Slack with Nimbella Commander
- Install Commander on your Mattermost Instance
- How to Fetch your Digital Ocean Billing Info on Slack?
- How to Stay Updated with Coronavirus Statistics on Slack?
- Create BlueJeans meetings on Mattermost using Commander
- How to Fetch your AWS Billing Info on Slack?
- Get your Datadog billing info in Slack with Nimbella Commander
- Serverless Slack Apps and Slash Commands
- How to use Slack Effectively with Nimbella Commander?
- How to Create a multi-user Chatroom Hosted on Serverless Cloud?
- Using Docker actions, running Golang, and other fun things with OpenWhisk
- The duality between serverless functions and APIs
- Serverless HTTP handlers with OpenWhisk
- Serverless functions in your favorite language with OpenWhisk
- Run Swiftly: precompiled Swift actions
- Performance debugging for serverless functions using the Cloud Shell
- Locally debugging OpenWhisk actions
- Composing functions into applications
- A Serverless Composition of Functions
- The Serverless Contract
- The dawn of the Cloud Computer
- Security and Serverless Functions