index.html
Код: Выделить всё
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service-worker.js", {
scope: "/",
// scope: "http://localhost:9999/"
});
}
send cors-request
document.querySelector("#sendApiRequest").addEventListener("click", () => {
const API = "http://localhost:9999/api/data";
fetch(API);
});
Код: Выделить всё
self.addEventListener("fetch", (event) => {
console.log("fetch event fired", event.request.url);
});
Код: Выделить всё
// Mock API server with NodeJs
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
const data = {
msg: 'success',
time: new Date().toLocaleString(),
}
res.end(JSON.stringify(data));
});
const port = 9999;
server.listen(port, () => {
console.log(`backend api on http://localhost:${port}`);
});
Подробнее здесь: https://stackoverflow.com/questions/798 ... t-in-webpa
Мобильная версия