简体   繁体   中英

Ajax request to run a NodeJS function that runs a bash script

This bash script makes a JSON file that I will need to parse and send that data to the client side java script to display it. I am trying to do this without refreshing the page, I also don't really want to use jquery. I have tried Axios but maybe I don't get it.

This is my Ajax request, I cant reach my NodeJs function with this, nor can i load the file because even though that is a direct path. I think this would work if I could run the function.

I have been working on this for like a week I just don't understand these AJAX request I guess. If you can help please do and give a in depth explanation.

    function digIt() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if(xhr.readyState === 4 && xhr.status === 200) {
            document.getElementById('mapSection').innerHTML = xhr.responseText;
        } else {
            alert(xhr.statusText);
        }
    };
    xhr.open('GET', "./../../middleware/index.js", true);
    console.log("done");
    canYouDigIt(domain);
    xhr.send();

};

This is my HTML , this is written in Jade/Pug.

 section(id="digToolWrapper")
        form(id="digToolInput")
            ul
                li #[input(id="digTool" name="domain" type="text" placeholder="Can you dig it?")]#[input(id="whois" value="whois" type="button" onclick="digIt(domain)")]

This is my middleware/index.js file.

    const shell = require('shelljs');


function loggedOut(req, res, next) {
    if (req.session && req.session.userId) {
        return res.redirect('/profile');
    }
    return next();
}

function checkForbidden(req, res, next) {
    if(! req.session.userId) {
        var err = new Error("You are not authorized to view this page.");
        err.status = 403
        return next(err);
    }
    return next();
}


// Whois Bash Script!!!
function canYouDigIt(domain) {
    shell.env["domain"] = domain;
     shell.exec(digIt.sh)
     console.log("here")
    };


module.exports.canYouDigIt = canYouDigIt;
module.exports.checkForbidden = checkForbidden;
module.exports.loggedOut = loggedOut;

This is my script I am trying to run for reference to understand what I am trying to do

domain=google.com
aRecord=$(dig -t a +short $domain)

ipWhois=$(whois $aRecord | awk '/NetRange/,0'| cut -d\# -f 1)

server=$(host $aRecord)

mxRecord=$(dig -t mx +short $domain)

nsRecord=$(dig -t ns +short $domain)

txtRecord=$(dig -t txt +short $domain)

ptrRecord=$(dig -x ptr +short $aRecord)

whoisRecord=$(whois $domain | awk '/Domain Status|Registrant Organization|Registry Expiry Date|Registration Expiration Date|Registrar:/')


serverType=$(curl -iA . -s $domain | awk  '/Server:/ {print $2}')

echo -e "{\n

\t \"domain\" :\n\"$domain\",\n
\t \"aRecord\" :\n\"$aRecord\",\n
\t \"ipWhois\" :\n\"$ipWhois\",\n
\t \"server\" :\n\"$server\",\n
\t \"mxRecord\" :\n\"$mxRecord\",\n
\t \"nsRecord\" :\n\"$nsRecord\",\n
\t \"txtRecord\" :\n\"$txtRecord\",\n
\t \"ptrRecord\" :\n\"$ptrRecord\",\n
\t \"whoisRecord\" :\n\"$whoisRecord\",\n

}" > ./whoisJson/whois$domain.json

Since you're trying to use Ajax, I am assuming you meant to run this script on the server, and deliver the output to the client to display? If so, the basic misunderstanding is that you have to actually set up a web server on the server side (using node?), that can receive a GET request from the client, invoke your script, collect its output, and send it to the client. Ajax "just" allows you to package up the request to the server to do that work for you, and delivers the answer to you.

OTOH, your code looks like you're trying to download the Javascript to the client, and then execute canYouDIgIt() on the client side. That is not how Ajax works (you cannot just call a method on the client that magically runs on the server, not without a lot of plumbing underneath you).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM