API (Application Programming Interface) development enables software applications to communicate with each other. This guide outlines the key aspects of designing, building, and deploying an API.
API (Application Programming Interface) development enables software applications to communicate with each other. This guide outlines the key aspects of designing, building, and deploying an API.
/v1/resource ).Set up the project:
mkdir my-api
cd my-api
npm init -y
npm install expressCreate the server:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
app.get('/api', (req, res) => {
res.send('Welcome to the API!');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});Add routes:
app.get('/api/items', (req, res) => {
res.json([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]);
});
app.post('/api/items', (req, res) => {
const newItem = req.body;
res.status(201).json(newItem);
});Run the server:
node server.jsYour email address will not be published. Required fields are marked *