I have been developing an express application that enables users to manage a list of web projects through various HTTP methods such as get, post, put, and delete. So far, I have successfully implemented the logic for handling get, delete, and post requests with no issues. However, I am facing a challenge in allowing users to edit the information stored in the array.
const express = require('express');
const app = express();
const port = 8080;
app.use(express.json());
let webProjects = [
{ID: 1, TITLE: "React Game", DESCRIPTION: "Tic tac toe game created using Create React App", URL: "http://heroku/myapp/game/"},
{ID: 2, TITLE: "Online Store", DESCRIPTION: "Online store created with HTML, CSS and Javascript.", URL: "http://git.com/myrepos/shop/index"}
]
app.get('/api', (req, res) => {
res.send(webProjects)
})
//the :id allows to capture a dynamic value in url
app.post('/api/:id/:title/:description/:url', (req, res) => {
const { id } = req.params;
const { title } = req.params;
const { description } = req.params;
const { url } = req.params;
const bob = {ID:id, TITLE:title, DESCRIPTION:description, URL:url}
webProjects.push(bob)
res.send(webProjects)
});
app.delete('/api/:id', (req, res) => {
const { id } = Number(req.params);
const index = webProjects.indexOf(id)
const deleteObj = webProjects.splice(index, 1)
res.send(`The item was successfully deleted`)
})
app.put('/api/', (res, req) => {
})
app.listen(port , () => {
console.log('Its working')
})