【问题标题】:How to get the details on backend nodejs with the GET request in angular如何使用角度的 GET 请求获取后端 nodejs 的详细信息
【发布时间】:2021-12-02 22:46:07
【问题描述】:

我无法在后端获取前端 GET 请求的查询参数。 我尝试使用 url 和查询。我需要在 nodejs 端获取查询。 请建议一种合适的方法,以帮助我使用 axios 获取有关 GET 请求的详细信息。

代码 -

component.ts - 角度文件

googleSearch(googleText){


let params = new HttpParams();
    params = params.append('q', googleText.trueLocation);
       

return new Promise((resolve, reject) => {
this.httpClient.get("http://localhost:3003/seekSearchApi" , ({params:params}))
      .pipe(map(Response => Response))
    
      .pipe(catchError(this.errorHandler))
      .subscribe((res: Response) => {

        this.writeItOutput = res;
        
        resolve(this.writeItOutput);

      });

    })
    
  

}
errorHandler(error: HttpErrorResponse) {
  return throwError(error.message || 'server Error');
}
}

server.js- 表达文件

app.use('/seekSearchApi', require('./server/nodejs-serverapi'));

applicationserver.js - nodejs 文件

function seekSearchApi(req,res) { 

var query = require('url').parse(req.url,true).query;

console.log("req.query.q", query.q);  //no response

console.log("Inside seekSearchApi");

  axios({
    method: 'get',
    url: 'https://serpapi.com/search.json?',
    data: {
      api_key: "xxxx",
      q:query.q
      hl: "en",
      gl: "us",
      google_domain: "google.com"
    }
  }).then((response) => {
    res.send(stringify(response))
   
  }, (error) => {
    console.log(error);
  });

【问题讨论】:

    标签: javascript node.js angular express


    【解决方案1】:

    我想通了。

    在节点侧。

    applicationserver.js

    function seekSearchApi(req,res) { 
    
    var url_data = url.parse(req.url, true);
    var query = url_data.query;
    var queryData= Object.assign({},query);
    console.log("queryData = ", queryData);
    
      ::::
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 2021-11-29
      • 2021-08-25
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      相关资源
      最近更新 更多