使用 navigator.sendBeacon 注销用户时出现错误 404

Error 404 when using navigator.sendBeacon to log out a user

hi 在我的应用程序中,如果用户关闭 tab/window 我想将他注销。为此,我按以下方式使用 navigator.sendBeacon

client.js:

window.addEventListener('unload', logData, false);
function logData() {
    var b = localStorage.localStor;
    if (b != null && b != "{}") {
        console.log("closed");
        console.log(JSON.parse(b).email); //prints user email
        navigator.sendBeacon("/log", { email: JSON.parse(b).email });
        localStorage.clear();
    }
}

server.js: (node.js)

var http = require('http');
var url = require('url');
var fs = require('fs');

var server = http.createServer(function (request, response) {

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE");
    var url_parts = url.parse(request.url);
    var path = url_parts.path;
    var postBody = [];  //for post uses
    if (path === "/log") {
        console.log("looging out"); // doesn't appear in node.js commad line
        postBody = [];
        request.on('data', (chunk) => {
            postBody.push(chunk);
        }).on('end', () => {
            postBody = Buffer.concat(postBody).toString();
            var parsedData = JSON.parse(postBody);
            var emailIndex = usersList.findIndex(function (element) {
                return element.email === parsedData.email;
            });
            console.log("length before :" + usersList.length); //nothing appear
            usersList.splice(emailIndex, 1);
            console.log("length before :" + usersList.length); //nothing appear

        });
    }

在应用程序中,我每 30 秒使用 settimeout 显示当前连接的用户数 usersList 的长度,但是当关闭 window 时,localStorage 确实很清楚但是用户仍然处于连接状态,因为连接的用户数没有改变。很明显,sendBeacon 以某种方式没有进入服务器,或者它进入了但不应用条件 if (path === "/log")。同样在关闭选项卡后,我在终端中收到以下错误(我在 Visual Code 中工作):

"POST /log" Error (404): "Not found"

我搜索了如何使用 sendbeacon 并像在 here 中那样使用它,但我认为由于我收到的错误,请求没有发送到服务器。

知道如何解决这个问题吗?

提前致谢!

一切都是对的,但是sendBeacon应该是这样的:

navigator.sendBeacon("http://localhost:8080/log", { email: JSON.parse(b).email });