Sr Technical Writer
JAMstack (JavaScript, APIs, and Markup) is a modern architecture for building fast and scalable web applications. It offers numerous benefits, including improved performance, enhanced security, and simplified maintenance.
This architecture is best suited for developers and organizations seeking to create dynamic, data-driven applications with a focus on user experience. Some common use cases for JAMstack include e-commerce platforms, blogs, and single-page applications.
In this tutorial, you’ll learn how to deploy a full-stack JAMstack application using:
By the end of this guide, you’ll have a working JAMstack app deployed on Netlify with a connected MongoDB database on DigitalOcean and by using the Netlify Extensions.
Before you begin, ensure you have:
First, you will create a Managed MongoDB cluster deployed on DigitalOcean. You can easily create production-ready MongoDB databases on DigitalOcean. For a step-by-step guide, watch this video:
Steps to create a Managed MongoDB cluster on DigitalOcean
You can also refer to this tutorial on How to Manage Data with MongoDB.
The steps are simple:
In this step you will install the DigitalOcean Netlify extension. You will use this extension to connect to your DigitalOcean account and start deploying and connecting cloud resources to your Netlify sites.
You need to login to your Netlify account. Sign up and go to Extensions tab from the sidebar.
Let’s start creating a full-stack JAMstack App.
You will use a React and Vite frontend template to interact with MongoDB.
Go to your terminal and use the below commands to setup an application template:
Note: If you don’t have Node.js and npm
installed, then you can refer the documentation on Installing Node.js and npm.
The above command should setup a basic application template.
Now go inside src
directory, create a file Api.js
to fetch data from the serverless function:
Let’s create a functions
directory inside the root directory of this project. You can read more about Netlify Functions.
Next, inside the functions
directory let’s create a file connectMongo.js
.
Initialize a Git repository in your project directory i.e., inside my-jamstack-app
:
Add all project files to Git:
Commit the changes:
Now, go to your GitHub account and create a new repository.
Next, add the remote origin:
Push your code to GitHub:
You will use the Netlify ClI to create a new “Site” and do a mannual deploy from command line.
Using Netlify CLI you configure continuous deployment straight from the command line. You can use Netlify CLI to run a local development server that you can share with others, run a local build and plugins, and deploy your site.
Let’s install the Netlify CLI:
Use the following command to deploy the site manually from command line.
Follow the on-sreen instructions to create a new site and other settings.
Now if you go to Netlify Sites, you should see a new Site created.
Now you will need to link the Git repository, created in the last step.
Go to Site Configuration from the left side panel and click “Build & deploy” and “Link repository”.
Select “Github” and add your repository and configure the build settings.
Now, select your Github repository:
Now configure and add build settings and then deploy the site.
Note: Make sure to set the “Functions Directory” to “/functions” which we created in this tutorial. You can leave the remaining build settings as default.
You can go to “Site Overview” to check the deployment status:
Now head over to “Extensions” and add the MongoDB connection String saved earlier in this tutorial.
The connection string will now be available in your Netlify project’s environment variables. It can now be accessed by your Netlify Functions.
You can also go to “Site Configuration” -> “Environment variables” and check the automatically added MongoDB environment variable by using the DigitalOcean extension.
Now let’s check the netlify function endpoint and perform some database operations.
You can find the function endpoint by going to Sites -> Logs -> Functions.
Use the below curl
command to store some data to the MongoDB database.
You should see the following output:
Let’s execute a GET
request to retrive the data:
You can also verify the POST
operation’s success by going to the function endpoint in your browser.
DigitalOcean’s Managed MongoDB provides several features that make it a powerful and convenient choice for developers:
To install the DigitalOcean Netlify Extension:
This integration simplifies the deployment and database management process for developers:
No, DigitalOcean provides a range of managed databases, including:
Security is a key focus of this integration, and it is handled through multiple layers:
Congratulations! You have successfully deployed a full-stack JAMstack application using DigitalOcean MongoDB and Netlify. Your application now features a practical React frontend that interacts with MongoDB.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!