• For people who are interested here is an example:

    First you look if the client is already logged in.
    If not you send a Header with Status 401 and WWW-Authenticate.

    // The header you check is "Basic Username:Password".
    if(req.headers.Authorization != "Basic YWRtaW46YWRtaW4=") {
    //Between the "" you can write a message that should than be displayed.
            res.writeHead(401, { 'WWW-Authenticate': 'Basic realm=""' });
            res.end();
    } else {
            //If logged in
    }
    

    Edit:

    Here is a little function.

    function basicAuth(req, res, username, password, message, callback) {
        var credentials = btoa(username + ':' + password);
        if(req.headers.Authorization != "Basic " + credentials) {
            res.writeHead(401, {
               'WWW-Authenticate': 'Basic realm="' + message + '"'
            });
            res.write("401 Unauthorized");
            res.end();
        } else {
            callback();
        }
    }
    
About

Avatar for Julian1 @Julian1 started