- Paigalda Node.js (kui on vaja)

2. Loo selles kaustas JavaScript-fail

3. Kirjuta kood, et saada andmed API-st
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const widgets = [
{ id : 1, name: "Jelizaveta", age:"21"},
{ id : 2, name: "Aljona", age:"32"},
{ id : 3, name: "Volodimir", age:"27"}
];
app.get('/widgets', (req, res) => {
res.send(widgets);
});
app.get('/widgets/:id', (req, res) => {
if (typeof widgets[req.params.id - 1] === 'undefined') {
return res.status(404).send({error: "Widget not found" })
}
res.send(widgets[req.params.id - 1])
})
app.post('widgets', (req, res) => {
if (!req.body.name || !req.body.age) {
return res.status(400).send({ error : "One or all params are missing"})
}
let newWidget = {
id: widgets.length +1,
name: req.body.name,
age: req.body.age
}
widgets.push(newWidget)
req.status(201)
.location('localhost:8080/widgets/' + (widgets.length -1))
.send(newWidget)
})
app.delete('/widgets/:id', (req, res) => {
if (typeof widgets[req.params.id -1] === 'undefined') {
return res.status(404).send({ error: "Widget not found"})
}
widgets.splice(req.params.id -1, 1)
res.status(204).send()
})
app.listen(8080, () => {
console.log('API up at: http://localhost:8080')
})
4. Lae alla laiendus nimega Postman

5.käivita oma rakendus terminalis

6. Postman UI-s klõpsake nuppu „New HTTP Request”.

7.Kasutajaliideses, kus Post või Get meetod paneb sinna sinu URL-i

8. Tulemust saate näha allpool

9. Kõik metoodid



Kokkuvõte
Kõik kasutavad päringud töötavad