0

I am using Angular to save form input to $scope.tag. I can't make a client side external API call with the form information as a parameter, so I need to pass it to the server to do. How can i achieve this?

The steps:

  1. user submits form
  2. client makes request to server
  3. server makes request to external API
  4. server sends response back to client

How can I achieve this?

  $scope.tag = '';

  // client side
  $http.get('/api')
    .then(function(response) {
      console.log(response);
  });

  // server side
  app.get('/api', function (req, res) {
  request('http://externalAPI.com/' + $scope.tag, function (req, res) {
    res.json(data);
    });
  });

1 Answer 1

3

You can do something like this:

// client side
  $http.post('/api', { tag: $scope.tag })
    .then(function(response) {
      console.log(response);
  });

  // server side
  app.post('/api', function (req, res) {
    console.log(req.query.tag);
    res.json({ status: 'success' });
  });

Just remember to include app.use(bodyParser.json()); before your routing middleware.

Sign up to request clarification or add additional context in comments.

3 Comments

and npm install body-parser and require it in the server js file since it comes separately now :P
This did not work. Using post gave me this error: Failed to load resource: net::ERR_CONNECTION_REFUSED
it seems like the post on the client side is not being made and giving me 404 (Not Found)

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.