// Import necessary modules and setup the Express app const express = require('express'); const fs = require('fs'); const app = express(); const wordsDataPath = './words-app/data/words.json'; const collectionsDataPath = './words-app/data/collections.json'; const PORT = process.env.PORT || 3000; // Endpoint to search for a certain number of words using a query text app.get('/api/words/:query', (req, res) => { let query = req.params.query; let count = req.query.count || 10; count = count > 100 ? 100 : count; // Limit to 100 words per request let words = fs.readFileSync(wordsDataPath); words = JSON.parse(words); let matchingWords = words.filter(word => word.includes(query)); let selectedWords = []; for (let i = 0; i < count && matchingWords.length > 0; i++) { let index = Math.floor(Math.random() * matchingWords.length); selectedWords.push(matchingWords[index]); matchingWords.splice(index, 1); } res.send(selectedWords); }); // Endpoint to read all collections app.get('/api/collections', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); res.send(collections); }); // Endpoint to create a new collection app.post('/api/collections', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); let newCollectionName = req.body.name; collections[newCollectionName] = []; fs.writeFileSync(collectionsDataPath, JSON.stringify(collections)); res.sendStatus(200); }); // Endpoint to get all words in a collection app.get('/api/collections/:name', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); let collectionName = req.params.name; let collection = collections[collectionName]; res.send(collection); }); // Endpoint to delete an empty collection app.delete('/api/collections/:name', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); let collectionName = req.params.name; if (collections[collectionName].length === 0) { delete collections[collectionName]; fs.writeFileSync(collectionsDataPath, JSON.stringify(collections)); res.sendStatus(200); } else { res.sendStatus(400); } }); // Endpoint to add a word to a collection app.post('/api/collections/:name/:word', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); let collectionName = req.params.name; let word = req.params.word; collections[collectionName].push(word); fs.writeFileSync(collectionsDataPath, JSON.stringify(collections)); res.sendStatus(200); }); // Endpoint to delete a word from a collection app.delete('/api/collections/:name/:word', (req, res) => { let collections = fs.readFileSync(collectionsDataPath); collections = JSON.parse(collections); let collectionName = req.params.name; let word = req.params.word; let index = collections[collectionName].indexOf(word); if (index > -1) { collections[collectionName].splice(index, 1); fs.writeFileSync(collectionsDataPath, JSON.stringify(collections)); res.sendStatus(200); } else { res.sendStatus(400); } }); // Start the server app.listen(PORT, () => { console.log(`server listening on port ${PORT}`); }); -please I need to
// Import necessary modules and setup the Express app
const express = require('express');
const fs = require('fs');
const app = express();
const wordsDataPath = './words-app/data/words.json';
const collectionsDataPath = './words-app/data/collections.json';
const PORT = process.env.PORT || 3000;
// Endpoint to search for a certain number of words using a query text
app.get('/api/words/:query', (req, res) => {
let query = req.params.query;
let count = req.query.count || 10;
count = count > 100 ? 100 : count; // Limit to 100 words per request
let words = fs.readFileSync(wordsDataPath);
words = JSON.parse(words);
let matchingWords = words.filter(word => word.includes(query));
let selectedWords = [];
for (let i = 0; i < count && matchingWords.length > 0; i++) {
let index = Math.floor(Math.random() * matchingWords.length);
selectedWords.push(matchingWords[index]);
matchingWords.splice(index, 1);
}
res.send(selectedWords);
});
// Endpoint to read all collections
app.get('/api/collections', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
res.send(collections);
});
// Endpoint to create a new collection
app.post('/api/collections', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
let newCollectionName = req.body.name;
collections[newCollectionName] = [];
fs.writeFileSync(collectionsDataPath, JSON.stringify(collections));
res.sendStatus(200);
});
// Endpoint to get all words in a collection
app.get('/api/collections/:name', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
let collectionName = req.params.name;
let collection = collections[collectionName];
res.send(collection);
});
// Endpoint to delete an empty collection
app.delete('/api/collections/:name', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
let collectionName = req.params.name;
if (collections[collectionName].length === 0) {
delete collections[collectionName];
fs.writeFileSync(collectionsDataPath, JSON.stringify(collections));
res.sendStatus(200);
} else {
res.sendStatus(400);
}
});
// Endpoint to add a word to a collection
app.post('/api/collections/:name/:word', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
let collectionName = req.params.name;
let word = req.params.word;
collections[collectionName].push(word);
fs.writeFileSync(collectionsDataPath, JSON.stringify(collections));
res.sendStatus(200);
});
// Endpoint to delete a word from a collection
app.delete('/api/collections/:name/:word', (req, res) => {
let collections = fs.readFileSync(collectionsDataPath);
collections = JSON.parse(collections);
let collectionName = req.params.name;
let word = req.params.word;
let index = collections[collectionName].indexOf(word);
if (index > -1) {
collections[collectionName].splice(index, 1);
fs.writeFileSync(collectionsDataPath,
JSON.stringify(collections)); res.sendStatus(200); } else { res.sendStatus(400); } });
// Start the server app.listen(PORT, () => { console.log(`server listening on port ${PORT}`);
});
-please I need to
Step by step
Solved in 5 steps with 1 images