A wrapper for the fetch API that simplifies HTTP requests
use npm:
npm i the-plug
or cdn:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/plug.js"></script>
const express = require('express')
const app = express()
const plug = require('the-plug')
app.get('/', async (req, res)=>{
const response = await plug.get('https://official-joke-api.appspot.com/random_joke')
res.json(response)
})
app.listen(3000)
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/plug.js"></script>
</head>
<body>
<h2>Random Joke</h2>
<p></p>
<p></p>
<script>
async function getJoke() {
const joke = await plug.get('https://official-joke-api.appspot.com/random_joke');
document.querySelectorAll('p')[0].innerHTML = joke.setup;
document.querySelectorAll('p')[1].innerHTML = joke.punchline;
}
getJoke();
</script>
</body>
</html>
To send data to a server, you can use the post
method:
const data = { name: "John Doe", age: 30 };
const response = await plug.post('https://your-api-endpoint.com/users', data, {
'Content-Type': 'application/json'
});
console.log(response);
To update existing data, use the put
method:
const updatedData = { name: "John Doe", age: 31 };
const response = await plug.put('https://your-api-endpoint.com/users/1', updatedData, {
'Content-Type': 'application/json'
});
console.log(response);
To delete data from a server, use the delete
method:
const response = await plug.delete('https://your-api-endpoint.com/users/1', {
'Authorization': 'Bearer your-token-here'
});
console.log(response);