hyprstack
hyprstack

Reputation: 4228

nodejs hapiJs: Sending, receiving data from client

I have the following files; client.js and server.js. I want to send data to my server, using ajax. I manage to send the searched username, but the domain is received on the server as undefined. I am not sure if I am missing something on the client side, or the server side, or both? On the server side, my function should be a generic function to allow it to receive any domain and the issue the request on that domain. Can anyone help out please?

Cliente:

$(document).ready(function(){
  console.log("Ready!");

  var domains=[ ]; //pass domain names into array for easier iteration
    domains.push($(".facebook").find("a").text());
    domains.push($(".github").find("a").text());
    domains.push($(".twitter").find("a").text());
    domains.push($(".instagram").find("a").text());
    domains.push($(".pinterest").find("a").text());
  console.log(domains);


  $("#searchbutton").on('click', function(event){

    var username = $("#searchname").val().trim(); // store value from searchbox
    console.log(username);

    if(username === ""){
      event.preventDefault();
    }

    if(username){
      var newhtml = "<p>";
      newhtml += username;
      newhtml += "</p>";
      $(".username").html(newhtml);
      $(".username").remove("newhtml");

      var domainCheck = function(domainName){
        $.ajax({
          url: "/"+username,
          type: "get",
          data: {domainName: domainName, username: username},
          success: function(response){
            console.log(domainName);
            console.log(response);
            }
        });
      };
      //send ajax request to server for each domain name to check for username availability
        var len = domains.length;
        for(var i = 0; i<len; i++){
          domainCheck(domains[i]);
          console.log(domains[i]+'\n');
        }
    }
  });
});

Server:

var Hapi = require('hapi');
var request = require('request');

var server = Hapi.createServer('localhost', 8080);



var routes =[
  {
    path: "/",
    method: "GET",
    handler: function (req, reply){
      console.log("Home page loaded and runnning!");
      reply.file('index.html');
    }
  },
  {
    path: '/{username}',
    method: 'GET',
    handler: function (req, reply){
// this is not working. the domain name is not being received from the client side. instead its passing undefined!
      request('http://www.'+ req.domain.domainName +'.com/' + req.params.username, function(error, response, body){
        console.log("Request received");
        console.log(response.statusCode);

        if ( response.statusCode === 404 ) {
          console.log( "Username " + req.params.username + " is available on " + req.domain.domains);
          reply({"available":"yes"});
        }

        if ( response.statusCode === 200 ) {
          console.log( "Username " + req.params.username + " is already taken on " + req.domain.domains);
          reply({"available":"no"});
        }
      });
    }
  },
  {
    method: 'GET',
    path: '/static/{param*}',
    handler: {
        directory: {
            path: 'static'
        }
    }
  }
];

server.route(routes);

server.start(function() {
    console.log("Server started", server.info.uri);
});


module.exports = server;

Upvotes: 0

Views: 1555

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42048

Change req.domain.domainName to req.query.domainName. When you access request data, you need to specify whether it is in the query, in the payload, etc.

Upvotes: 3

Related Questions