使用初始请求的所有参数转发用户请求nodejs

时间:2016-02-24 17:48:16

标签: javascript node.js http request

我想重定向用户请求,以便调用其他服务:

  1. 当用户使用所有参数(标题和正文)到达localhost:8080 / test时,请调用http://localhost:3000/api/user/isAuthenticated api
  2. 处理该api的响应。 对于标题,下面的代码运行良好,但我不能让它适用于body参数,我使用bodyParse和in / test函数我有整个初始请求localhost:8080 / test

     app.all('/test', function (req, res) {
    
                //req.body all parameters set for the initial request localhost:8080/test
    
                var options = {
                url: 'http://localhost:3000/api/user/isAuthenticated',
                method: req.method,
                headers: req.headers,
                form: {
                    body:JSON.stringify(req.body)
                }
            };
    
            request.post(options, function (err, httpResponse, body) {
    
                //body undefinded if i use form: {    body:JSON.stringify(req.body) } 
                //The body is passed i have something like {"body":"{\"email\":\" ...  when i do req.body in the api controller but not the right format and with the key body so i need to do something like req.body.body in the api controller 
    
                //but work if i do not pass the body and delete form: { body:JSON.stringify(req.body)  }  i have the response of my api in the body variable below
                console.log("CONSTOLE " + body)
    
                if (body["authenticated"] == true) {
                    return res.json(body)
                } else {
                    return res.json(body)
                }
            });
        });
    
  3. 有没有办法传递请求的所有参数直接调用另一个api。

0 个答案:

没有答案