Skip to content
Snippets Groups Projects
Commit 885ed4d2 authored by Robin Keskisärkkä's avatar Robin Keskisärkkä
Browse files

add example that uses JSON body in POST requests

parent f9f91cf3
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
function getUsingXMLHttpRequest(){ function getUsingXMLHttpRequest(){
let xhttp = new XMLHttpRequest(); let xhttp = new XMLHttpRequest();
xhttp.onload = () => { xhttp.onload = () => {
document.getElementById('1').innerHTML = xhttp.responseText document.getElementById('1').innerHTML = JSON.parse(xhttp.responseText).counter
} }
xhttp.open('GET', '/counter', true); xhttp.open('GET', '/counter', true);
xhttp.send(); xhttp.send();
...@@ -16,31 +16,37 @@ ...@@ -16,31 +16,37 @@
function postUsingXMLHttpRequest(){ function postUsingXMLHttpRequest(){
let xhttp = new XMLHttpRequest(); let xhttp = new XMLHttpRequest();
xhttp.onload = () => { xhttp.onload = () => {
document.getElementById('2').innerHTML = xhttp.responseText console.log(xhttp.responseText)
document.getElementById('2').innerHTML = JSON.parse(xhttp.responseText).counter
} }
xhttp.open('POST', '/counter', true); xhttp.open('POST', '/counter', true);
xhttp.send(); xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(JSON.stringify({'add' : 1}));
} }
function getUsingFetch(){ function getUsingFetch(){
fetch('/counter', { method: 'GET'}).then( resp => { fetch('/counter', { method: 'GET'}).then( resp => {
resp.text().then( text => { resp.text().then( data => {
document.getElementById('3').innerHTML = text document.getElementById('3').innerHTML = JSON.parse(data).counter
}) })
}) })
} }
function postUsingFetch(){ function postUsingFetch(){
fetch('/counter', { method: 'POST'}) fetch('/counter', {
.then( resp => { method: "POST",
return resp.text() headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({'add': 1})
}) })
.then( text => { .then( resp => resp.json() )
document.getElementById('4').innerHTML = text .then( data => {
document.getElementById('4').innerHTML = data.counter
}) })
} }
window.addEventListener('load', function() { window.addEventListener('load', () => {
getUsingXMLHttpRequest() getUsingXMLHttpRequest()
postUsingXMLHttpRequest() postUsingXMLHttpRequest()
getUsingFetch() getUsingFetch()
......
...@@ -2,18 +2,23 @@ const express = require('express') ...@@ -2,18 +2,23 @@ const express = require('express')
let app = express() let app = express()
app.use(express.static('public')) app.use(express.static('public'))
// Parses all incoming messages of type "application/json" (i.e. body of requests will
// appear to be JSON by default)
app.use(express.json())
let counter = 0; let counter = 0;
app.route('/counter') app.route('/counter')
.get((req, res) => { .get((req, res) => {
res.status(200) res.status(200)
res.send(`Count: ${counter}`) res.setHeader('Content-Type', 'application/json')
res.json({ counter }) // short form of { 'counter': counter }
}) })
.post((req, res) => { .post((req, res) => {
counter += req.body['add'];
res.status(300) res.status(300)
counter++ res.setHeader('Content-Type', 'application/json')
res.send(`Count: ${counter}`) res.json({ counter }) // short form of { 'counter': counter }
}) })
app.listen(3000, () => { app.listen(3000, () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment