1

I am trying to return value from an input box to a variable in route, by taking this source as a reference but am stuck, in the example author is searching a keyword, constructing a url using this keyword, and returning a body response.

My use case is a little different I need user to provide a url as a string in search box, which I would then pass to my request function to spit response body

Current app.js (server-side)

app.get('/searching', function(req, res){

  // input value from search
  var url = req.query.search;
  console.log(url); // prints value

  request({ uri: url}, function (error, response, body) {
    if (!error) {
      content = body;
      console.log(content);
    } else {
        console.log('Oops! Error when contacting slack.com');
    }
  });


  res.send(content);
});

main.js (client-side)

    $(function(){
     $('#submit').on( 'click', function () {
        var sSearch = $('#search').val();
        var parameters = { search: sSearch };
           $.get( '/searching',parameters, function(data) {
           $('#results').html(data);
        });
    });
   });

I realize /searching in both above files must be replaced because currently its trying to 'search' the user entered url value as a query string, so if I enter "https://google.com" in the text box the application tries to search:

http://localhost:3000/searching?search=https%3A%2F%2Fgoogle.com

instead I need it to pass this url as is to the request function so that I can print the body response, which in this case would be the page's source code. But am not sure how should I proceed

index.jade (template)

extends layout

block content
    input#search(type="search", placeholder="Enter Keyword")
    button#submit(type='submit',class='btn btn-success') Search
    h2#results

    script(src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js")
    script(src="/javascripts/main.js")

How should I format so that I can pass the variable from client to server and then send it back to client after processing? Any help is appreciated.

1
  • You are leaking content to the global namespace by declaring it without using the var keyword by the way. Commented Jul 12, 2015 at 6:52

2 Answers 2

2

In the app.js you are making a async http call, but the response is sent before the http callback is invoked. Just move the res.send(content); inside the call back

app.get('/searching', function(req, res){

   // input value from search
  var url = req.query.search;
  console.log(url); // prints value

  request({ uri: url}, function (error, response, body) {
    if (!error) {
      content = body;
      console.log(content);
    } else {
      console.log('Oops! Error when contacting slack.com');
    }
    res.send(content);
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

ah thanks for pointing that I out. Though the issue persists, the request module is still trying to search on http://localhost:3000/searching?search=https\\google.com though I only want the request module to process the url that the user has entered and not append it as a query string.
Check value of uri that is sent to request module. There was a typo in your code, I changed that to url from uri.
That was not a typo, urlis the variable that am passing which holds user input value. var url = req.query.search;
Looks like the question is edited and fixed now. Anyway, I am able to use the above code and serve the content of the req.query.search
0

i see ajax request in your client code , so in your server code, try to response something like this :

  res.status(200).json(content)

in your client code ,check if the content is there (console.log(data) before $().html(data)... )

PD : request is async , so you have to response inside request callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.