async function newBookX() {
async function postDataX(url = "", data = {}) {
// Default options are marked with *
//const response = await fetch(url, {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', 'https://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
const response = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "same-origin",//"cors", // no-cors, *cors, same-origin
cache: "no-cache",//"force-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
headers
//'Content-Type': 'application/json'
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *client
body: //"fdsfsdf"
JSON.stringify({
bokTitel: "tjöta 123"
}), // body data type must match "Content-Type" header
});
// return response.json(); // parses JSON response into native JavaScript objects
return response.text();
}
for (var i=0;i<20;i++)
{
var a1 = new Date();
//postDataX("https://pacific-mountain-79072.herokuapp.com" + "/books/").then((data) => {
//postDataX("http://localhost:3000" + "/books/").then((data) => {
postDataX("/books/").then((data) => {
var a2 = new Date();
var a3 = a2 - a1;
document.getElementById("hej").innerHTML +=
data + ". Anropet tog " + a3 + " ms
";
});
}
}
OCH DENNA LIGGER I app.js på servern:
app.post("/books", (req, res) => {
console.time();
let theText = req.body;//let t = book.bokTitel.toUpperCase();
console.timeEnd();
res.end("NY! PELLE KALLE 123777RESULTAT FRÅN SERVER (ändrar 240429, och 250211):" + theText);
});
OBS: FÖR ATT starta om node funkar detta - kanske lite väl omständligt, men:
pm2 kill
caddy stop
caddy start
pm2 start