Serverless functions are a cloud computing execution model where developers write and deploy small, single-purpose functions without managing the underlying infrastructure. These functions run on-demand in stateless containers and automatically scale based on usage.
Would you like a hands-on example of how to write a serverless function? 🚀
Sure! Here’s a simple serverless function for Vercel using Node.js that responds with "Hello, World!" when accessed.
shnpm install -g vercel
shvercel init
Inside your Vercel project, create an
api
directory (if it doesn’t exist) and
add a file called hello.js
:
api/hello.js
javascriptexport default function handler(req, res) { res.status(200).json({ message: "Hello, World! 🚀" }); }
shvercel deploy
https://your-project.vercel.app/api/hello
Response:shcurl https://your-project.vercel.app/api/hello
json{ "message": "Hello, World! 🚀" }
If you want to handle POST requests,
modify hello.js
like this:
javascriptexport default function handler(req, res) { if (req.method === "POST") { res.status(200).json({ message: `Hello, ${req.body.name || "Guest"}!` }); } else { res.status(405).json({ error: "Method Not Allowed" }); } }
Now, you can send a POST request with JSON data:
shcurl -X POST https://your-project.vercel.app/api/hello -H "Content-Type: application/json" -d '{"name": "Mehdi"}'
Response:
json{ "message": "Hello, Mehdi!" }
Let me know if you need more customizations! 🚀
Serverless functions, like those deployed on Vercel, AWS Lambda, or Cloudflare Workers, are ideal for executing small, event-driven tasks without managing servers. They are particularly useful for backend logic, handling API requests, and automating workflows.
shhttps://your-app.vercel.app/api/user?id=123
✅ No Server Management – Focus on
writing code, not maintaining servers.
✅
Auto-Scaling – Handles high traffic
automatically.
✅
Cost-Effective – Pay only for what
you use (no idle server costs).
✅
Security – Limited execution time
reduces attack risks.
✅
Integration-Friendly – Works with
databases, APIs, and third-party services easily.
Would you like a real-world example of a specific use case? Let me know! 🔥
Here's a basic backend using Vercel serverless functions that provides a simple CRUD API for managing a list of tasks (To-Do List).
If you haven't already, install the Vercel CLI:
shnpm install -g vercel
Then initialize your project:
shvercel init
Inside your Vercel project, create an
api
folder, and inside it, create a file
named tasks.js
.
api/tasks.js
- Simple CRUD API
javascriptlet tasks = []; // In-memory storage (resets on every function execution) export default function handler(req, res) { if (req.method === "GET") { return res.status(200).json(tasks); } if (req.method === "POST") { const { task } = req.body; if (!task) { return res.status(400).json({ error: "Task is required" }); } const newTask = { id: tasks.length + 1, task }; tasks.push(newTask); return res.status(201).json(newTask); } if (req.method === "DELETE") { const { id } = req.query; tasks = tasks.filter((t) => t.id !== parseInt(id)); return res.status(200).json({ message: "Task deleted" }); } return res.status(405).json({ error: "Method Not Allowed" }); }
Run the following command in your project directory:
shvercel deploy
Once deployed, you'll get a URL like:
https://your-project.vercel.app/api/tasks
shcurl -X GET https://your-project.vercel.app/api/tasks
shcurl -X POST https://your-project.vercel.app/api/tasks -H "Content-Type: application/json" -d '{"task": "Learn Vercel"}'
shcurl -X DELETE "https://your-project.vercel.app/api/tasks?id=1"
Would you like me to add database support or a frontend UI for this? 🔥