It is a good software engineering practice to separate portions of your code function to work independently of other parts. Since your codes are made up of functions basically, serverless functions allow you to deploy those functions without the complexity of managing a server to run them.
In this tutorial, we will be learning what serverless functions are, how to write them, and how to deploy them on Netlify in minutes. We will also touch up on other uses of serverless functions.
A serverless function is a function hosted on a cloud infrastructure and performs a single purpose. It is very similar to every other function you write as long as it has its own dependencies and can run on its own. It is serverless because it takes off the stress of server maintenance and scaling the function from you.
Netlify has made it very easy to write any backend task and deploy it independently or alongside your project in simple steps. Your functions can also be deployed by a simple git push
command if linked with your GitHub repository. These functions are called Netlify Functions.
Serverless functions work just like your API endpoint
Create an empty folder either manually or by running mkdir custom-netlify-functions
. Use any name you feel like. Here I’m using ‘custom-netlify-functions’.
cd custom-netlify-functions
touch readme.md
git init
to initialize folder with Git repositorygit add . && git commit -m "initial commit"
to add the file and commit changes.git remote add origin git@github.com:YourGithubName/your-repo-slug.git
git push -u origin master
to push your filesWe will be using the Netlify CLI as it makes it super easy to get our project up and running Run npm install -g netlify-cli
If you don’t have a netlify account, create one now
While inside the folder, run netlify init
to initialize the folder with netlify.
The prompt will ask you “What would you like to do?”, select “Create and configure a new site”. This option creates a new site on your netlify account.
If you are already authenticated, it will ask you to select your team (Netlify Account). If you aren’t authenticated on netlify.com yet, it will open a browser and request that you enter your login credentials.
The next prompt will ask your Site name and if you’ll like to authenticate with Github. For site name, retain the same name as ‘custom-netlify-functions’ and authenticate with Github
This step connects your Netlify account and repo with the same one you just created on GitHub.
Next prompts. For “Your build command”, simply press Enter key. For Directory to deploy to, press Enter key too to use “.” (the current directory).
#netlify #serverless #javascript #nodejs #function