Getting Started with CRUD Operations Using REST API
REST API, or Representational State Transfer Application Programming Interface, is a set of architectural principles and constraints used for designing networked applications. It provides a standardized way for systems to communicate over HTTP by defining a set of conventions for creating, updating, retrieving, and deleting resources.
HTTP Methods
RESTful APIs use standard HTTP methods for operations:
- GET: Retrieve a resource.
2. POST: Create a new resource.
3. PUT: Update a resource (replacing it).
4. PATCH: Update a resource (partially).
5. DELETE: Delete a resource.
Here’s a basic example using Node.js and Express for creating a simple RESTful API with CRUD operations.
1. Setting Up Your Project :
# Create a new Node.js project
npm init -y
# Install necessary dependencies
npm install express body-parser
2. Creating Your Server (index.js) :
const express = require(’express’);
const bodyParser = require(’body-parser’);
const app = express();
const PORT = 3000;
// Middleware to parse JSON
app.use(bodyParser.json());
// Sample in-memory database
let resources = [
{ id: 1, name: 'Resource 1' },
{ id: 2, name: 'Resource 2' },
];
// CRUD Operations
// Read all resources
app.get(’/api/resources’, (req, res) => {
res.json(resources);
});
// Read a specific resource by ID
app.get(’/api/resources/:id’, (req, res) => {
const resourceId = parseInt(req.params.id);
const resource = resources.find((r) => r.id === resourceId);
if (resource) {
res.json(resource);
} else {
res.status(404).json({ error: 'Resource not found' });
}
});
// Create a new resource
app.post(’/api/resources’, (req, res) => {
const newResource = req.body;
resources.push(newResource);
res.status(201).json(newResource);
});
// Update a resource by ID
app.put(’/api/resources/:id’, (req, res) => {
const resourceId = parseInt(req.params.id);
const updatedResource = req.body;
resources = resources.map((r) =>
r.id === resourceId ? { ...r, ...updatedResource } : r
);
res.json({ message: 'Resource updated successfully' });
});
// Delete a resource by ID
app.delete(’/api/resources/:id’, (req, res) => {
const resourceId = parseInt(req.params.id);
resources = resources.filter((r) => r.id !== resourceId);
res.json({ message: 'Resource deleted successfully' });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
3. Testing Your API :
Use tools like Postman or cURL to test your CRUD operations.
Create: Send a POST request to http://localhost:3000/api/resources with a JSON body.
Read: Send GET requests to http://localhost:3000/api/resources to get all resources or http://localhost:3000/api/resources/:id to get a specific resource.
Update: Send a PUT request to http://localhost:3000/api/resources/:id with a JSON body.
Delete: Send a DELETE request to http://localhost:3000/api/resources/:id.
This is a basic example to help you get started. In a real-world scenario, you would typically connect to a database, implement validation, and handle edge cases more thoroughly.