provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline such as requests and responses.
fetch('http://localhost:3000/tv-shows')
.then(response => {
// collection of TV Shows (Array)
return response.json()
})
.then(tvShows => {
console.log(tvShows);
});
async function fetchTVShows() {
const response = await fetch('http://localhost:3000/tv-shows');
const tvShows = response.json();
console.log(tvShows);
}
fetch('http://localhost:3000/tv-shows')
.then(response => {
return response.json();
}, err => {
// Network error, server could not be reached
console.log(err);
});
fetch('http://localhost:3000/tv-shows')
.then(response => {
if (response.status === 404 || response.status === 500) {
console.error(response.statusText);
} else {
return response.json();
}
})
.then(tvShows => {
console.log(tvShows);
});
fetch('http://localhost:3000/tv-shows')
.then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
})
.then(tvShows => {
console.log(tvShows);
})
.catch(err => {
console.error(err);
});
function handleErrors(response) {
if (!response.ok) { throw Error(response.statusText); }
return response;
}
fetch('http://localhost:3000/tv-shows')
.then(handleErrors)
.then(tvShows => console.log(tvShows))
.catch(err => console.error(err));
function postData(url, data) {
return fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(data)
})
.then(response => response.json());
}
const newTvShow = { title: 'Silicon Valley' };
postData('http://localhost:3000/tv-shows', newTvShow)
.then(addedTvShow => console.log(addedTvShow));
function putData(url, data) {
return fetch(url, {
method: 'PUT',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(data)
});
}
const updatedTvShow = { id: 1, title: 'Silicon Valley' };
putData('http://localhost:3000/tv-shows/1', updatedTvShow)
.then(response => console.log(response.status)); // 204
function deleteData(url, data) {
return fetch(url, { method: 'DELETE' });
}
deleteData('http://localhost:3000/tv-shows/1')
.then(response => console.log(response.status)); // 204
$ npm install axios
$ npm install socket.io
const express = require('express');
const app = express();
// Create Express HTTP server
const server = require('http').createServer(app);
// Create Websocket
const io = require('socket.io')(server);
server.listen(3000, () => {
console.log('Listening on port 3000...');
});
io.on('connection', socket => {
console.log('A user is connected!');
socket.on('disconnect', () => {
console.log('A user is disconnected!');
});
});
io.on('connection', socket => {
socket.on('message/new', message => {
console.log(`Message received: ${message}`);
});
});
io.on('connection', socket => {
socket.emit('hello', 'Hello, World!');
});
io.on('connection', socket => {
socket.broadcast.emit('hello', 'Hello, World!');
});
io.on('connection', socket => {
io.emit('hello', 'Hello, World!');
});
<script src="/socket.io-client/dist/socket.io.js"></script>
<script>
// Open connection
const socket = io.connect('http://localhost:3000');
// Handle 'hello' event / message
socket.on('hello', message => {
console.log(`Message received from server: ${message}`);
});
</script>
<script src="/socket.io-client/dist/socket.io.js"></script>
<script>
const socket = io.connect('http://localhost:3000');
socket.on('hello', message => {
socket.emit('message/new', { message: 'Hey, there!' });
});
</script>