The correct way to implement the server side of an AJAX request in Node.js

I am creating a search engine that queries the server side Twitter and Wiki after the client submits a search string. When I was still completely client-side, the AJAX request to the Wiki API looked like this:

$.ajax({
    url: 'https://en.wikipedia.org/w/api.php',
    data: {
        action: 'query',
        list: 'search',
        srsearch: searchString, // variable pulled from input
        format: 'json',
        //origin: 'https://www.mediawiki.org'
    },
    xhrFields: {
        withCredentials: true
    },
    dataType: 'jsonp' // will probably use 'json' now that I'm server-side
}).done( function ( data ) {
    searchParser(data);
});
... //searchParser() below

Now I have implemented the Node.js web application, which takes the search string from the POST client form on the main page (index.ejs). The server will then display a results page (results.ejs) with the wiki and twitter search results. The search on the Twitter API is already being processed by BoyCook TwitterJSClient .

I would like to know the best way to fulfill this exact request from my server. I looked around and could not find a short answer. Does anyone have any advice on this?

+4
2

, , :

function wikiSearch () {

    var query = querystring.stringify({
        action: 'query',
        list: 'search',
        srsearch: searchString,
        srlimit: 10,
        srwhat: 'text',
        format: 'json'
    });
    var options = {
        hostname: 'en.wikipedia.org',
        path: '/w/api.php?' + query
    };

    var req = https.request(options, function(res) {

        if (!res) { 
            wikiData = "An error occured!";
            complete();
        };

        var body = '';
        console.log("statusCode: ", res.statusCode);
        res.setEncoding('utf8');

        res.on('data', function(data) {
            body += data;
        });

        res.on('end', function() {
            wikiData = wikiDataParser( JSON.parse(body) );
            complete();
        });
    });

    req.end();
    req.on('error', function (err) {
        wikiData = "<h2>MediaWiki API is currently down.</h2>";
        complete();
    })

};
... // complete() is below

wikiData .

+1

MEAN. , , - , NPM, api. , , node.js:

npm install twitter

. , , .

//include this....loads the twitter client api app...will make this a breeze
        var Twitter = require('twitter');

    //your access keys and info to login into the api....fill them.
        var client = new Twitter({
          consumer_key: '',
          consumer_secret: '',
          access_token_key: '',
          access_token_secret: ''
        });

    //the below code executes when you hit this path

    app.post('/getTweetsYo', function (res, req, data) {

// gets username from ajax post of user data....access via req.data. Passes it to twitter api request
        var params = {screen_name: req.data.username };
        client.get('statuses/user_timeline', params, function(error, tweets, response){
          if (!error) {
            console.log(tweets);

    // sending the tweets back to the front end!

    res.send(tweets);

          }
    });
0

Source: https://habr.com/ru/post/1610209/


All Articles