繁体   English   中英

节点/快速/ Angular Server端外部API请求

[英]Node / Express / Angular Server side external API request

我正在使用Angular将表单输入保存到$ scope.tag。 我无法使用表单信息作为参数进行客户端外部API调用,因此我需要将其传递给服务器。 我怎样才能做到这一点?

步骤:

  1. 用户提交表格
  2. 客户端向服务器发出请求
  3. 服务器向外部API发出请求
  4. 服务器将响应发送回客户端

我该如何实现?

  $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);
    });
  });

您可以执行以下操作:

// 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' });
  });

只记得包括app.use(bodyParser.json()); 在路由中间件之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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