I am trying to implement an API where it will read from a text file, display the response, can add new entries and edit the ones inside.
Loading
I am trying to implement an API where it will read from a text file, display the response, can add new entries and edit the ones inside.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 9, 2023, 8:12 AM EDT
To implement an API that reads from a text file, displays the response, adds new entries, and edits existing ones, you can follow these general steps. Since you haven't specified the programming language or framework you're using, I'll provide a high-level approach that you can adapt to your chosen technology stack.
Choose a Programming Language and Framework: Decide on the programming language and framework you want to use to build the API. Popular choices include Node.js with Express, Python with Flask or Django, C# with ASP.NET Core, etc.
Create API Endpoints: Define the API endpoints that will handle reading, displaying, adding, and editing entries. For example:
GET /entries: Retrieve all entries from the text file and return as JSON.GET /entries/:id: Retrieve a specific entry by ID.POST /entries: Add a new entry to the text file.PUT /entries/:id: Update an existing entry.File Operations: Implement functions to read from and write to the text file:
API Implementation: Implement the API endpoints using the chosen framework. Here's an example using Node.js and Express:
const express = require('express'); const fs = require('fs/promises'); const app = express(); const port = 3000; // Middleware to parse JSON in requests app.use(express.json()); // Read entries from the file async function readEntries() { const data = await fs.readFile('entries.json', 'utf-8'); return JSON.parse(data); } // Write entries to the file async function writeEntries(entries) { await fs.writeFile('entries.json', JSON.stringify(entries, null, 2)); } // Get all entries app.get('/entries', async (req, res) => { const entries = await readEntries(); res.json(entries); }); // Get a specific entry app.get('/entries/:id', async (req, res) => { const entries = await readEntries(); const entry = entries.find(e => e.id === req.params.id); res.json(entry); }); // Add a new entry app.post('/entries', async (req, res) => { const entries = await readEntries(); const newEntry = req.body; entries.push(newEntry); await writeEntries(entries); res.status(201).json(newEntry); }); // Update an existing entry app.put('/entries/:id', async (req, res) => { const entries = await readEntries(); const updatedEntry = req.body; const index = entries.findIndex(e => e.id === req.params.id); if (index !== -1) { entries[index] = updatedEntry; await writeEntries(entries); res.json(updatedEntry); } else { res.status(404).json({ error: 'Entry not found' }); } }); app.listen(port, () => { console.log(`API server is running on port ${port}`); });Testing and Postman: Test your API using tools like Postman or by sending HTTP requests from your application.
File Format and Data Structure: Decide on the format of the text file and the structure of the data you'll store in it (e.g., JSON). Your API endpoints should read and write data in this format.